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.
Related
In our project we start using JSON file, when i start research on What the JSON is and where it comes from, What is the technology they have used, is it only javascript or anyother and which parsing tool they have used,but i didn't get any clear information. If anyone have such reference document and ideas please give me link .
JSON is just a data exchange format, like xml, csv...
Even if it's part of JavaScript it's not restricted to JS. JSON can be used in nearly every language and im pretty sure, every good language have a parser implementation in place. json.org lists a overview of projects related to JSON.
As you asked for links to information:
(Complete Overview)
http://json.org/
(Douglas Crockford 'The JSON Saga') https://www.youtube.com/watch?v=-C-JoyNuQJs
(ECMA Standard that defines JSON) http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
I'm a Frontend Engineer, our team is switching many of our old services to micro services written in clojure. The main issue I'm seeing is that clojure naming conventions prefer hyphens to-separate-words in variable names. This means if you straight map variables into JSON any JS consumer would need to access this data using bracket notation e.g. response['to-separate-words']. This is obviously not ideal. I thought this would be a easy best practice to lookup but I've been looking for an hour and it seems like all the docs I read avoid this issue but using single words. Has anyone else dealt with this.
You might use camel-snake-kebab library which supports most of the combinations. You can plug it in into most of the JSON libraries for Clojure (cheshire, cli-json, data.json - as mentioned by Elogent) as they usually have an option to provide a function for handling property name mangling.
For example with cheshire:
Generate JSON with camel case property names:
(cheshire.core/generate-string {:my-clojure-key "abc"}
{:key-fn camel-snake-kebab.core/->camelCaseString})
Result:
{"myClojureKey":"abc"}
Parse JSON to get map with kebab case keys:
(cheshire.core/parse-string "{\"myClojureKey\":\"abc\"}"
camel-snake-kebab.core/->kebab-case-keyword)
Result:
{:my-clojure-key "abc"}
There is also an example for data.json in camel-snake-kebab readme.
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.
Recently came across the term JSONC in a YouTube API. I browsed the Web, but found nothing much about it. Can someone explain whether these two are the same or different?
There is also jsonc aka "JSON with comments", created by Microsoft and used by Visual Studio Code. The logic for it can be found here, alas without exhaustive specification (though I'd like to be proven wrong on this).
On top of that there is this project with an actual specification which is also called jsonc, but also does far more than just adding comments.
While there definitely is a use for these technologies, some critical thinking is advised. JSON containing comments is not JSON.
JSON-C seems to just be a variation of JSON mainly targeted at C development. I.e., from the open source docs, "JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects."ref^1
From the YouTube API perspective (specifically, version 2, not the new version 3), The JSON-C response is just a condensed version of the JSON response (removing "duplicate, irrelevant or easily calculated values").ref^2
Why would the JSON response have "duplicate, irrelevant or easily calculated values" values anyway? Because it is converting the original ATOM XML format directly to JSON in a loseless conversion. You can find out more details here.
However, I would suggest using version 3 of the YouTube Data API. It is much easier to use. =)
JSONC is an open-source, Javascript API created by Tomás Corral Casas for reducing the size of the amount of JSON data that is transported between clients and servers. It uses two different approaches to achieve this, JSONC.compress and JSONC.pack. More information can be found on the JSONC Github page:
https://github.com/tcorral/JSONC
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.