Friday, November 11, 2011

MJSON 1.1 Released

NOTE: The Library described here has grown and moved to Github with official website http://bolerio.github.io/mjson/It still remains faithful to the original goal, it's still a single Java source file, but the API has been polished and support for JSON Schema validation was included. And documentation is much more extensive.

I few months, we made an official release of a compact, minimal, concise JSON library that we called MJSON. See the JSON Library blog post for an introduction to this library. After some experience with it, some bug reports on that blog post, we have released an improved, fully backward compatible version 1.1. The original download & documentation links now point to the new version.
Pointers

Documentation: http://www.sharegov.org/mjson/doc
Download: http://www.sharegov.org/mjson/mjson.jar
Source code: http://www.sharegov.org/mjson/Json.java
Maven
The latest 1.1 release is available on Maven central:
<dependency>
    <groupId>org.sharegov</groupId>
    <artifactId>mjson</artifactId>
    <version>1.1</version>
</dependency>
Also, both 1.0 and 1.1 versions are available from our Maven repository at http://repo.sharegov.org/mvn. This is depracated and henforth we'll be publishing only on Maven central. Incude that repository in your POM or in a settings.xml profile like so:
<repositories>
  <repository>
  <releases>
  <enabled>true</enabled>
  </releases>
  <snapshots>
  <enabled>true</enabled>
  </snapshots>
  <id>sharegov</id>
  <url>http://repo.sharegov.org/mvn</url>
  </repository>
  </repositories>
Then include a dependency like so:
<dependency>
  <groupId>mjson</groupId>
  <artifactId>mjson</artifactId>
  <version>1.1</version>
  <scope>compile</scope>
</dependency>
List of Improvements

The following bugs were fixed in this new release:
  • The example from the Javadocs was missing a final 'up()' call.
  • The NumberJson implementation appropriately return true in its isNumber method now.
  • A parsing bug.
  • Some warnings were removed and explicitly disabled.
The following additional features were implemented:
  • Addition of  top-level methods: is(String, Object) for objects and is(int, Object) for arrays. Those methods return true if the given named property (or indexed element) is equal to the passed in Object as the second parameter. They return false if an object doesn't have the specified property or an index array is out of bounds. For example is(name, value) is equivalent to 'has(name) && at(name).equals(make(value))'.
  • Addition of a dup() method that will clone a given Json entity. This method will create a new object even for the immutable primitive Json types. Objects and arrays are cloned (i.e. duplicated) recursively.
  • Addition of a Factory interface that allows plugging of your own implementation of Json entities (both primitives and aggregates) as well as customized mapping of Java objects to Json. More on this below.
The Factory Interface - Customizing MJSON

The Factory interface, declared as an inner interface, within the scope of the Json class, looks like this:
public static interface Factory
{
Json nil();
Json bool(boolean value);
Json string(String value);
Json number(Number value);
Json object();
Json array();
Json make(Object anything);
}

You can implement this interface if you need to customize how the Json types are actually represented. For instance, objects are represented using standard Java HashMap. But you may want to has a different representation, say a LinkedHashMap or a more efficient variant, optimizing for strings (say, a Trie based map of some sorts). Or you may want strings to be case-insensitive in which case you'd have a Json derived class representing strings, but whose equals method will actually do equalsIgnoreCase etc. 

The make allows you to customize how arbitrary Java objects are translated into Json. It should be easy for example to implement a make method that handles Java beans with introspection, which is something that we don't want by default as part of the API.

The methods in that interface are used internally every time a new Json instance has to be constructed, either from a known type, as a default empty structure or from an arbitrary Java type. Thus you can pretty much customize the representation any way you like while relying on the same simple API.

The default implementation of this factory is public: Json.DefaultFactory. Therefore you can extend that default implementation and only customize certain aspects of the Json representation. 

That's it for this release. Enjoy!

Cheers,
Boris

5 comments:

  1. Found a bug in the Json.make function, check array component class case "double" (is "boolean" not "double").


    else if (boolean.class == comp)
    for (double b : (double[])anything) A.add(b);

    ReplyDelete
  2. Ah, thanks! Boolean is actually handled above, so this should be else if (double.class == comp) etc..

    Will be fixed in next release :)

    ReplyDelete
  3. mjson is absolutely fantastic. After hours of searching the Internet, I finally stumbled across this and it did exactly what I wanted without the traditional verboseness of Java. Thank you :)

    ReplyDelete
  4. You can purchase the new and latest models in just a few clicks without
    the expense and hassle of trips to the hobby store. Besides, we feature an exotic variety of products
    manufactured in different parts of Asia, which you won't be able to get elsewhere!

    rctophobby.com

    ReplyDelete
  5. NOTE: The Library described here has grown and moved to Github with official website http://bolerio.github.io/mjson/. It still remains faithful to the original goal, it's still a single Java source file, but the API has been polished and support for JSON Schema validation was included. And documentation is much more extensive.

    ReplyDelete