5

So my code is a client of an api, the data is returned as xml and Ive been able to create valid xsd file from some examples of that xml and then generate some JAXB classes from the schema so my code can now load and work with the xml data without ever having to work directly with Xml.

But unfortunately in the latest version of the api they have dropped xml support and only return json. Is there a json process I can do analogous to my xml process ?

If its not possible from a schema are there solutions so that if I manually create my json classes, I can them use them to automatically martial in raw json data, so at least I only have to deal with json once.

EDIT:Maybe https://github.com/ko5tik/jsonserializer would be useful

UPDATE:FYI so I looked at jsonschema2pojo but that only creates pojos from a schema, and I didnt actually have a json schema, just the actual json. I had a go at creating a schema from the example json I had but didnt get it working for al but the simplest example.

I then looked at http://wiki.fasterxml.com/JacksonInFiveMinutes , Jackson would have been able to use the pojos created by jsonschema2pojo if Id managed to get it working. So I then tried following the example and created a POJO based on the json data I had and it was quite easy, then I then tried full data binding using this and it worked first time.

So in summary Im going to use Jackson for dealing with json returned by the webservice, I'll have to manually create a POJO for each entity but at least it works.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

4 Answers4

9

As to answer, maybe try "jsonschema2pojo".

But similar questions have already been asked before:

Community
  • 1
  • 1
StaxMan
  • 113,358
  • 34
  • 211
  • 239
1

If you want to create POJOs from a sample JSON file, I've created a (hacky) ruby script that can help. I've added more details in another answer here: https://stackoverflow.com/a/8585423/1109175

You can find the code on github: https://github.com/wotifgroup/json2pojo

Community
  • 1
  • 1
Chris R
  • 199
  • 2
  • 2
0

No, I don't see a way for doing that... JSON is without a schema definition, so there's no meta-data to generate the classes from.

Edit: I stand corrected, there is a schema definition (draft), it has however expired since the beginning of this year, and I have not encountered any schemas provided.

Anders
  • 660
  • 3
  • 12
  • Hmm, that is what I feared so why is everyone moving away from xml to IMO a worse solution ? – Paul Taylor Nov 04 '11 at 11:49
  • 2
    Because of the fact that the data-structure of JSON is much more lightweight, and easier for "inferior" devices to handle (such as cell phones, etc.). Furthermore, the datastructure can be directly included into Javascript (no need to write a custom deserialization mechanism). JSON = JavaScript Object Notation. – Anders Nov 04 '11 at 11:52
  • 1
    but Im not writing in Javascript (thank goodness), I really think they should provide both – Paul Taylor Nov 04 '11 at 12:00
  • I agree... supplying both a SOAP and RESTFul interface is the way to go. And the task is trivial (can be a matter of providing annotations or a snippet of XML configuration in Java) – Anders Nov 04 '11 at 12:06
  • I have yet to understand fondness people have for Schemas -- in many cases, it just adds complexity without adding value: you MUST generate client code, systems is more rigid and closely coupled (schema changes, you MUST re-generate code; very hard to maintain backwards compatibility). Part of the reason for lack of interest in JSON schema languages (which do exist -- see JSON Schema) is that there is not as much need as what many thing. – StaxMan Nov 04 '11 at 17:50
  • Answer is incorrect, for simple reasons that JSON Schema (http://json-schema.org/) exists; plus, XML similarly "has no schema": one can use a Schema with XML, but XML document instances do not have in-built schema (they can have DTD). – StaxMan Nov 04 '11 at 17:51
  • 1
    @StaxMan You are incorrect on so many points, allow me to elaborate: XML can indeed have a build-in (inlined) schema - both the form of XSD and DTDs. If you took the time to look at the JSON schema site you've linked to, and read the schema draft you'd see it had expired on May 26th 2011. The fondness of schemas, or my fondness of them is that you (the consumer and provider) agree to a contract - which makes data exchange SO much easier. Having a schema, furthermore makes machine validation possible, which means that no human verification is necessary. – Anders Nov 05 '11 at 20:05
  • @Anders We disagree at almost every level so I won't bother disputing things point by point. But I will point that your original statement is factually incorrect -- it is of no relevance whatsoever whether JSON document instance refers to schema -- even XML schema refs from XML instance documents is considered a bad practice. Rather one associates schema with instance document in JSON _exactly_ like one would do in XML; there is absolutely no requirement for that metadata to be included within instance document. – StaxMan Nov 06 '11 at 02:28
  • 1
    @StaxMan a schema gives the client a way of defining the data they are receiving, if there is no schema and you change the data you send then the clientstill has to change, but its difficult to work out what has changed – Paul Taylor Nov 08 '11 at 09:52
  • My main contention is in using data-format schemas, since IMO that is the wrong tool (low-level, focuses on data representation, not data model). Not the concept of contracts being used, which is useful. However beyond this there is the question of when and how these contracts would be used, regardless of forms they take. – StaxMan Nov 09 '11 at 01:03
0

I don't think there is a JSON equivalent to XSD, and that makes what you are asking for really hard to implement. For a start, a generator program cannot reliably deduce the Java types that need to be used for the attributes of a JSON object. (In the XML case, you made those deductions and expressed them in the corresponding XSD.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • JSON Schema (http://json-schema.org/) has been around for quite a while... granted, it may depend on definition of equivalency, but it has lots of similarities. – StaxMan Nov 04 '11 at 17:48