JSON Parser recommendation: Is there a JSON Parser which maintains order? - java

I know a JSON that maintains order is no longer JSON, but this is a requirement in some use and throw code. I have read other hacks like replacing HashMap with LinkedHashMap in the JSONObject.java file. I was wondering if there is a parser which does this without any modifications.

You could use a streaming JSON parser, e.g. Jackson, which will deliver the tags as it finds it. Of course that means that you might have to change your program logic to process the JSON elements on the fly.

Related

How to convert JsonForm to Json?

I would like to extract only the data present in a Json to JsonForm using Java.
Which framework can I use to this operation?
I've found the answer. Don't let the ugly aspect of JsonForm deceive you. I don't know if this apply for all cases, but I just had to extract the Json object that contains only the data. This can be done using any common Json parser, as like as Gson.

Should I use JSONReader for complex json streams?

I've been trying to parse a very complex JSON blob with several levels of nesting. The structure of the JSON returned by the server can change for every call.
I have written a recursive JSON parser using org.json, but the performance isn't great. I do know that JSONReader offers better performance and have been trying to write a module that parses this complex JSON recursively using JSONReader.
From what I see, JSONReader is better for streams where the structure of the JSON is known. Is it advisable to use this for JSON that can change continuously? The example in the Android docs (and other examples i found online) assumes that the JSON has a fixed structure.

Using custom inputSplit and/or recordReader classes for reading JSON

In my hadoop class they are wanting us to learn how to write/configure custom input format classes to handle data that isn't simply one record per line (like TextInputFormat).
The data is JSON data from the Twitter API, so it is basically a list of small JSON objects each representing tweets.
[
{
"text":"tweet string",
...more...
},
{
....
},
]
The lab spec suggests focusing on writing a custom recordReader class that manually parses JSON in the given InputSplit, using something like a stack to keep track of opening/closing braces. Frankly this seems idiotic to me, I should not have to reinvent the wheel of parsing JSON... that's the advantage to using a standard data format.
Instead I was hoping to come up with a better solution using an existing JSON parser like json-simple. It seems I should be able to make a custom InputSplit class that splits the list of JSON objects into smaller sublists, and then the custom recordReader would also use the JSON parser to get each JSON object individually.
Is this a proper approach? Should I be customizing an InputSplit? Is there a better way to make a large JSON text consumable by a Hadoop MapReduce job?

What is the fastest way to deserialize JSON in java

I'm wondering what is the fastest way to parse json in JAVA ?
Getting a default object graph using the library built in Array, and Object objects
Getting a custom object graph using your own java bean
Thanks
Well, The newest and wickedly Fastest one is Boon Json. I used it in my project and got an improvement of 20X. I actually got scared and double checked to see if Library is functionally correct. Thankfully, it is :) :)
Boon has built in methods to serialize and de-serialize from/to Java Array/Maps and Custom Beans.
More Here : https://github.com/RichardHightower/boon
Mapping parsed JSON to Java bean involves additional steps, so using the raw interface (e.g. the streaming API of Jackson) will be faster. This way, you can also read until have what you need and stop parsing.
In response to #sikorski
From Jackson Wiki:
Data binding is built using Streaming API as the underlying JSON
reading/writing system: as such it has high-performance [...], but has
some additional overhead compared to pure streaming/incremental
processing
This is pretty much inevitable. If you are writing a generic Jackson parser, you obviously can't use custom types in it. Therefore it follows that you'll have to construct the custom type after you read the JSON with the generic parser, and hence the generic parser will be faster. It's worth noting though that such overhead is very small and almost never something you need to optimize away.
You can use json-simple its a high performance library and its very easy to use :
JSONParser parser=new JSONParser();
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=parser.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=parser.parse(s);
System.out.println(obj);
s="[5,]";
obj=parser.parse(s);
System.out.println(obj);
s="[5,,2]";
obj=parser.parse(s);
System.out.println(obj);

Java String to JSON and then JSON to array

I have the following String code and I want to convert to json and iterate through items and put them into an arraylist in Java.
I also have a class called Users whith its attributes and getters/setters(id, nick, age, online, avatar)how should i do:
code:
//this is the real code
String a = "{"id":"1","nick":"jhon","age":20,"online":1,"avatar":"http:\/\/www.example.com\/image.jpeg"},{"id":"2","nick":"mike","age":45,"online":0,"avatar":"http:\/\/www.example.com\/image.jpeg"},{"id":"3","nick":"carl","age":12,"online":1,"avatar":"http:\/\/www.example.com/image.jpeg"},{"id":"4","nick":"ana","age":22,"online":0,"avatar":"http:\/\/www.example.com\/image.jpeg"}";
//this is what i want to do
String a = real code sample;
Json b = a.toJson; // something like this
Arraylist<User> list = new Arraylist<User>();
for each b{
list.add( new user(b.getId(),b.getNick()....));
}
i want to do something like that and of course the code is an example and here it's not well written.
You may prefer to use Gson library which has good documentation for how to use. You can easly parse objects to JSON and JSON to object. So it may be a better alternative for your own parser.
It also supports Arrays and Collections.
I think you need to validate you array first on JSONLINT
Jackson is quite nice for this sort of thing - I've been using it for some time with no problems. I've heard nice things about Gson as well, but had no reason to switch from Jackson. Xstream also can work with Json.
Instead of working with a special "JSON" object, Jackson (and I believe Gson as well) stuff the JSON values into a POJO. You use the annotations to customize how the values are read and written.
FWIW, JSON processing is easy with a library like this, but also something you don't want to do much - it's relatively expensive in terms of CPU. It's not that the libs are slow - that kind of text processing is a lot of work.

Categories