json-lib - how many classes does JSONSerializer require? - json

I'm trying to parse some JSON data, and I'm using the examples from this site: http://answers.oreilly.com/topic/257-how-to-parse-json-in-java
I'm using the data from section 2 and the code from section 3, and json-lib as my JSON library.
But when I tried to run the sample, the JSONSerializer class about a missing a language class from Apache Commons. I downloaded that, and it complained of a missing logging class. I downloaded that, and it complained of the missing EZmorph class. I downloaded that, and it complained of a missing Collections class. I then downloaded an unofficial JAR file with all the Apache Commons components, and it complained of a missing SLF4J logger or something. At least point, I gave up and decided to try Google's GSON instead.
This seems much more complicated than it should be. Just how many packages do I need do download? Is there a way to disable logging? Or am I doing something wrong?

I would recommend using another, more commonly used Java JSON library: for example Jackson or GSON. There are lots of tutorials and articles on using both, and I don't think there are many reasons to use json-lib over either one.

You can also have a look at Genson.
It is a all in one library, with nice features, easy to use and with good performances.
For example to serialize a full pojo to json and then deserialize it back :
Genson genson = new Genson();
String json = genson.serialize(yourPojo);
// and deserialize it
YourPojo pojo = genson.deserialize(json, YourPojo.class);
As Staxman says, there is no reason to use json-lib. I am still surprised of how many people are using it when there a couple of better libraries.

Related

Newtonsoft.Json as configuration provider in ASP.NET (5+)

For better or worse our codebase relies heavily on Newtonsoft.Json. There are various type converters etc. based on this framework and it is simply not worth the effort to rewrite them using some other JSON framework (if even possible).
We use appsettings.json (+ other custom files) to load settings into our applications.
Typically we used to configure this like:
configurationBuilder.AddJsonFile(pathToFile, ...);
Now, we need to use some of the converters we usually rely on when parsing regular JSON for the settings JSON as well. These are usually being automatically picked up by Newtonsoft.Json so we thought the solution would simply be to reference
Microsoft.Extensions.Configuration.NewtonsoftJson and change to:
configurationBuilder.AddNewtonwoftJsonFile(pathToFile, ...);
However this does not appear to be the case. The converters are not being invoked when we call.
var someSettings = configurationSection.Get<SomeSettings>();
If we paste the same settings into a string and manually parse it the usual Newtonsoft.Json way. This works just fine.
Our conclusion is that either we are doing something wrong (hopefully) or the "binding" part where a configuration section is transferred into actual properties on the object is not part of the Newtonsoft.Json configuration extension.
Any suggestions?

Configure Play/Jerkson to deserialize single quoted (non-standard) JSON w/o duplicating a lot of code

In a Play 2.0 application, I need to deserialize some JSON from a source which I don't control which uses single-quotes around strings -- where the JSON spec calls for double-quotes.
The solution using Jackson is here:
Configure Jackson to deserialize single quoted (invalid) JSON
But trying to implement this solution in play2.0 I hit a wall of static objects and private classes... it should be enough to replace object JerksonJson with one implementing the solution linked above at initialization, but because it is a static object it can't be extended, and id I try to copy it into my code I need to drag along classes PlaySerializers, PlayDeserializers, JsValueDeserializer,... I stopped here, as it looked like too much.
Is there a clean solution?
How about trying to fix the invalid json string by replacing every ' in it with a "?
That would work if 's are only used for specifying strings.
I realize that this may not help too much with Play framework part, but perhaps you could use Jackson Scala Module instead of Jerkson? Doing that should make it easier to just use ObjectMapper, with Scala module registered, instead of having to use Jerkson-specific handlers.

Single serialization layer to Json with Casbah/Salat

I am trying to create a serialization layer which allows me to:
Store my classes in a MongoDB data source
Convert them to JSON to use them in a REST API.
Some classes are clearly not case classes (because they are inherited from a Java codebase) and I would have to write ad-hoc code for that. Is registering a BSON Hook for my non standard type the correct approach, and does it provide Json serialization?
Salat maintainer here.
You might prefer to create a Salat custom transformer instead of registering a BSON hook with Casbah.
See simple example and spec.
If you run into any issues, feel free to ping the mailing list with a small sample Github project that demonstrates what isn't working.

How to encode JSON in ABAP before 7.02

As Horst Keller mentioned in his ABAP and JSON post, "with Releases 7.02 and 7.03/7.31 (Kernelpatch 116) JSON is supported natively in ABAP".
Appartently 7.02 in my case of too generic because the line below:
writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
returns the error: "The field CO_XT_JSON is unknown, but there is a field with the similar name CO_XT_XOP".
So is there any way to easily generate JSON?
Edit: Screenshot from SAP - Status
About the class CL_TREX_JSON_SERIALIZER: I also used this class during developping a mobile sap application and I found the created JSON not being valid, thus I started googling and found this http://scn.sap.com/community/mobile/blog/2012/09/24/serialize-abap-data-into-json-format (which also explains how to create a valid JSON serializer).
Validate your json with json lint http://jsonlint.com/ to see if it is valid.. otherwise, thats for sure, you get a lot of trouble in debugging why it doenst work and dont get the point that the serializer is corrupt. regards, zY
take a look at the ZCL_MDP_JSON Library. You can parse/encode any JSON. So, it is best suited for JSON scenarios that requires flexibility.
It is easy to understand if you have used JSON in other languages. You only need to study methods of ZCL_MDP_JSON_NODE class once & look at the examples.
Here is an extended overview of the library:
http://scn.sap.com/community/abap/blog/2016/07/03/an-open-source-abap-json-library--zclmdpjson
GitHub repo with examples directory: https://github.com/fatihpense/zcl_mdp_json
Disclaimer: I'm the author of the project. If you have questions, don't hesitate to contact me.
Here is some code I wrote for ABAP data <-> JSON conversion some time ago before the new capabilities were included with ABAP (or maybe it was just an older system).
https://gist.github.com/mydoghasworms/2291540
Include the code in your ABAP source and use the method data_to_json of the class.
A nice overview of custom ABAP <-> JSON serializers including yet another one can be found in this blog post
Most popular from my point of view is SE38's ZJSON-library which can be installed using SAPLINK (and which - in contrast to many others) has an explicit license attached to it: Apache 2.0
If upgrading to a newer patch isn't an option in the short term, you can also use class CL_TREX_JSON_SERIALIZER to serialise objects to JSON. A little bit of a quick-and-dirty solution but it works well.

Object mapper from and to json

I am developing an application system that has multiple executable applications on different platforms (java and .net).
For communication between them I am using JSON format. So I need to map object to and from json very frequently. Current solution (seems workaround) is jackson at java end and Newtonsoft.Json at .NET end. Problem is property name are not same and not all properties will be required at de-serialization end
So my questions are:
1. Is there any mapper to do this.
Currently using NewtonSoft.JSON.DatasetMapper at .Net end and
jsonanysetter annotation at java, but in this approach mapping
definition is loaded for each object as actual object mapping code
is in code. For example:
//C#
myobj.prop1 = dataSet.Tables[0].Rows[0]["propertyName1"].ToString();
// and so on.....
//Java
switch(key)
{
case "prop1":
myobj.setPropery1(value.toString());
break;
//and so on......
}
2. Object transformationRate needs to be very high as object are
sent and recieved at very high speed. say some 10k objects per second.
We used GSON in one of our project , i think this reference may help you, Apart from it ,there is a similar question may help you. another q/a in stackoverflow
You should take a look at Jackson. It's the de facto JSON library for Java and will happily handle turning objects into JSON and back again. It has many options to allow you to alter the output, and most per-object configuration is carried out using annotations so is visible in your model rather than hidden away in a separate configuration file.