Git Product home page Git Product logo

Comments (4)

GoogleCodeExporter avatar GoogleCodeExporter commented on June 11, 2024
JSON.simple currently doesn't support direct serialization of Java arrays, you 
need
to use java.util.List instead:

List list = new ArrayList();
list.add("1");
org.json.simple.JSONValue.toJSONString(list);

Please refer the following link for more information:
http://code.google.com/p/json-simple/wiki/MappingBetweenJSONAndJavaEntities

Thanks.

Original comment by [email protected] on 20 Feb 2009 at 4:39

  • Changed state: Invalid

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 11, 2024
Hi,

I have read the link you posted, but the array that I want to encode to 
JSON-string
is the a part of a composite object that I can't work (easily) with.

I worked on a _simple_ encoder (working with arrays too):

// ------------------
@SuppressWarnings("unchecked")
public static String toJSString(final Object object) {

    if (object == null)
        return "null";

    final StringBuilder sb = new StringBuilder();

    if (object instanceof Map) {
        final Map<Object, Object> map = (Map<Object, Object>) object;
        sb.append('{');
        final Set<Entry<Object, Object>> entrySet = map.entrySet();
        for (Iterator iterator = entrySet.iterator(); iterator.hasNext();) {
            final Entry<Object, Object> entry = (Entry<Object, Object>) iterator
                    .next();

            sb.append(entry.getKey().toString());
            sb.append(':');
            sb.append(toJSString(entry.getValue()));
            if (iterator.hasNext())
                sb.append(",\n");

        }
        sb.append('}');
    } else if (object instanceof Object[]) {
        final Object[] obj = (Object[]) object;
        sb.append('[');
        for (int i = 0; i < obj.length; i++) {
            sb.append(toJSString(obj[i]));
            if (i + 1 < obj.length)
                sb.append(',');
        }
        sb.append(']');
    } else if (object instanceof Collection) {
        final Collection obj = (Collection) object;
        sb.append('[');
        for (Iterator iterator = obj.iterator(); iterator.hasNext();) {
            Object object2 = (Object) iterator.next();
            sb.append(toJSString(object2));
            if (iterator.hasNext())
                sb.append(',');

        }
        sb.append(']');
    } else if (object instanceof Number) {
        sb.append(object);
    } else {
        sb.append('"').append(object.toString().replaceAll("\"", "\\\\\""))
                .append('"');
    }

    return sb.toString();
}
// ------------------

It's far from complete/normative but it simply works :)

Regards,
Alejandro.

Original comment by atec.post on 20 Feb 2009 at 8:47

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 11, 2024
Sorry, I forgot to say that java.lang.Array class is final and it can't 
implement
JSONAware interface.

I have seen other JSON libraries that uses a "registration" model to customize 
the
output.
In example, some kind of: 
JSONAwareFactory.registerJSONAware(java.lang.Array.class,
new MyArrayJSONAware());

It could be nice to implement this feature to complete the output customization 
service.

Regards,
Alejandro.

Original comment by atec.post on 20 Feb 2009 at 8:58

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 11, 2024
Yes, each library strikes to keep the balance among convenience, performance and
neatness of the code...We'll consider this kind of feature in next step of this 
project:

Auto marshalling and unmarshalling utils for Java Beans 

Now please feel free to use your modified version. Thank you for the feedback.

Original comment by [email protected] on 20 Feb 2009 at 10:39

from json-simple.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.