Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Exclamation Serialize array in java and unserialize in php

    Hello,

    Basically I want to serialize the array such that I can unserialize it in php. I want to send this array via webservice and consume it in php and unserialize to get back that array. Any algorithm to make structure of array serialized like php serialize ?

    Thanks,
    Chintan Vyas

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I remember making a utility a while ago that handled general purpose serialization of Java things into a PHP serialization kind of string. I had read a reference on PHP serialized syntax. It is a delimited string expression similar to JSON.

    This utility doesn't handle Arrays, e.g String[]. I guess at the time I didn't need arrays. But it does handle List objects. I would guess the PHP serialized form of a Java List and a Java Array would be the same notation anyway, so maybe you can adapt this, or just ship it a list of items

    Java Code:
    // created on 2009-10-24
    
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    /**
     * A utility to convert Java objects into PHP serialized strings.
    
     * <table border="1" style="border-collapse: collapse;">
     * <caption>PHP's Serialization Format</caption>
     * <thead>
     * <tr><th>Type</th><th>serialized</th><th>Example</th></tr>
     * </thead>
     * <tbody>
     * <tr><td>NULL</td><td>N;</td><td>N;</td></tr>
     * <tr><td>Integer</td><td>i:$data;</td><td>i:123;</td></tr>
     * <tr><td>Double</td><td>d:$data;</td><td>d:1.23;</td></tr>
     * <tr><td>Float</td><td>d:$data;</td><td>d:1.23;</td></tr>
     * <tr><td>Boolean</td><td>b:$bool_value;</td><td>b:1;</td></tr>
     * <tr><td>String</td><td>s:$data_length:"$data";</td><td>s:5:"Hello"</td></tr>
     * <tr><td valign="top">Array</td><td>a:$key_count:{$key;$value}<br/> $value can be any data type</td><td valign="top">a:1:{i:1;i:2}</td></tr>   
     * </table>
     * @author thein
     *
     */
    public class PhpSerialize
    {
        
        /**
         * Converts a Java integer to PHP serialized notation.
         * @param javaInt
         * @return
         */
        public static String serialize(Integer javaInt) {
            if (javaInt == null) {
                return "N;";
            }
            return "i:" + javaInt.toString() + ";";
        }
        
        /**
         * Converts a java double to PHP serialized notation.
         * @param javaDouble
         * @return
         */
        public static String serialize(Double javaDouble) {
            if (javaDouble == null) {
                return "N;";
            }
            return "d:" + javaDouble.toString() + ";";
        }
        
        public static String serialize(Boolean javaBoolean) {
            if (javaBoolean == null) {
                return "N;";
            }
            return "b:" + (javaBoolean.equals(Boolean.TRUE) ? 1 : 0) + ";";
        }
        
        /**
         * Converts a Java string into a PHP serialized notation.
         * @param javaString
         * @return
         */
        public static String serialize(String javaString) {
            if ( javaString == null) {
                return "N;";
            }
            return "s:" + javaString.length() + ":\"" + javaString + "\";";
        }
        
        /**
         * Converts a Java list into a PHP serialized notation
         * @param aList
         * @return
         */
        public static String serialize(List<Object> aList) {
            if (aList == null) {
                return "N;";
            }
            StringBuffer buf = new StringBuffer();
            buf.append("a:").append(aList.size()).append(":{");
            int offset = 0;
            for (Iterator<Object> it = aList.iterator(); it.hasNext();) {
                buf.append(serialize(new Integer(offset ++)));
                Object value = it.next();
                buf.append(serialize(value));            
            }
            buf.append("};");
            return buf.toString();
        }
        
        public static String serialize(Map<Object,Object> aMap) {
            if (aMap == null) {
                return "N;";
            }
            StringBuffer buf = new StringBuffer();
            buf.append("a:").append(aMap.size()).append(":{");
            for (Iterator<Object> it = aMap.keySet().iterator(); it.hasNext();) {
                Object key = it.next();
                buf.append(serialize(key));
                Object value = aMap.get(key);
                buf.append(serialize(value));
            }
            buf.append("};");
            return buf.toString();
        }
        
        
        public static String serialize(Object value) {
            if (value == null) {
                return "N;";
            }
            if (value instanceof Integer) {
                return serialize((Integer) value);
            }
            
            if (value instanceof Double) {
                return serialize((Double) value);
            }
            
            if (value instanceof Boolean) {
                return serialize((Boolean) value);            
            }
            
            if (value instanceof List<?>) {
                return serialize((List<?>) value);
            }
            
            if (value instanceof Map<?,?>) {
                return serialize((Map<?,?>) value);
            }
            
            return serialize((String) value);
            
        }
    }

  3. #3
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by travishein View Post
    I remember making a utility a while ago that handled general purpose serialization of Java things into a PHP serialization kind of string. I had read a reference on PHP serialized syntax. It is a delimited string expression similar to JSON.

    This utility doesn't handle Arrays, e.g String[]. I guess at the time I didn't need arrays. But it does handle List objects. I would guess the PHP serialized form of a Java List and a Java Array would be the same notation anyway, so maybe you can adapt this, or just ship it a list of items

    Java Code:
    // created on 2009-10-24
    
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    /**
     * A utility to convert Java objects into PHP serialized strings.
    
     * <table border="1" style="border-collapse: collapse;">
     * <caption>PHP's Serialization Format</caption>
     * <thead>
     * <tr><th>Type</th><th>serialized</th><th>Example</th></tr>
     * </thead>
     * <tbody>
     * <tr><td>NULL</td><td>N;</td><td>N;</td></tr>
     * <tr><td>Integer</td><td>i:$data;</td><td>i:123;</td></tr>
     * <tr><td>Double</td><td>d:$data;</td><td>d:1.23;</td></tr>
     * <tr><td>Float</td><td>d:$data;</td><td>d:1.23;</td></tr>
     * <tr><td>Boolean</td><td>b:$bool_value;</td><td>b:1;</td></tr>
     * <tr><td>String</td><td>s:$data_length:"$data";</td><td>s:5:"Hello"</td></tr>
     * <tr><td valign="top">Array</td><td>a:$key_count:{$key;$value}<br/> $value can be any data type</td><td valign="top">a:1:{i:1;i:2}</td></tr>   
     * </table>
     * @author thein
     *
     */
    public class PhpSerialize
    {
        
        /**
         * Converts a Java integer to PHP serialized notation.
         * @param javaInt
         * @return
         */
        public static String serialize(Integer javaInt) {
            if (javaInt == null) {
                return "N;";
            }
            return "i:" + javaInt.toString() + ";";
        }
        
        /**
         * Converts a java double to PHP serialized notation.
         * @param javaDouble
         * @return
         */
        public static String serialize(Double javaDouble) {
            if (javaDouble == null) {
                return "N;";
            }
            return "d:" + javaDouble.toString() + ";";
        }
        
        public static String serialize(Boolean javaBoolean) {
            if (javaBoolean == null) {
                return "N;";
            }
            return "b:" + (javaBoolean.equals(Boolean.TRUE) ? 1 : 0) + ";";
        }
        
        /**
         * Converts a Java string into a PHP serialized notation.
         * @param javaString
         * @return
         */
        public static String serialize(String javaString) {
            if ( javaString == null) {
                return "N;";
            }
            return "s:" + javaString.length() + ":\"" + javaString + "\";";
        }
        
        /**
         * Converts a Java list into a PHP serialized notation
         * @param aList
         * @return
         */
        public static String serialize(List<Object> aList) {
            if (aList == null) {
                return "N;";
            }
            StringBuffer buf = new StringBuffer();
            buf.append("a:").append(aList.size()).append(":{");
            int offset = 0;
            for (Iterator<Object> it = aList.iterator(); it.hasNext();) {
                buf.append(serialize(new Integer(offset ++)));
                Object value = it.next();
                buf.append(serialize(value));            
            }
            buf.append("};");
            return buf.toString();
        }
        
        public static String serialize(Map<Object,Object> aMap) {
            if (aMap == null) {
                return "N;";
            }
            StringBuffer buf = new StringBuffer();
            buf.append("a:").append(aMap.size()).append(":{");
            for (Iterator<Object> it = aMap.keySet().iterator(); it.hasNext();) {
                Object key = it.next();
                buf.append(serialize(key));
                Object value = aMap.get(key);
                buf.append(serialize(value));
            }
            buf.append("};");
            return buf.toString();
        }
        
        
        public static String serialize(Object value) {
            if (value == null) {
                return "N;";
            }
            if (value instanceof Integer) {
                return serialize((Integer) value);
            }
            
            if (value instanceof Double) {
                return serialize((Double) value);
            }
            
            if (value instanceof Boolean) {
                return serialize((Boolean) value);            
            }
            
            if (value instanceof List<?>) {
                return serialize((List<?>) value);
            }
            
            if (value instanceof Map<?,?>) {
                return serialize((Map<?,?>) value);
            }
            
            return serialize((String) value);
            
        }
    }
    Thanks travishein !!!
    This snippet really helped me. I have to modify according to my req.. Thanks again for your quick reply.

    Regards,
    Chintan Vyas.

Similar Threads

  1. Using JSTL to capture HTTP referer data and serialize into a variable
    By jeremy.wilson in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-19-2011, 12:44 PM
  2. Serialize this!
    By dustybray in forum Advanced Java
    Replies: 1
    Last Post: 12-03-2010, 01:31 AM
  3. Replies: 3
    Last Post: 10-15-2010, 03:03 PM
  4. To serialize or not to serialize?
    By XmisterIS in forum New To Java
    Replies: 2
    Last Post: 09-21-2010, 01:37 PM
  5. serialize to web service?
    By theartz in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2008, 01:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •