Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generating types 'by example', using example JSON document instead of a JSON Schema #42

Closed
joelittlejohn opened this issue Jun 23, 2013 · 10 comments

Comments

@joelittlejohn
Copy link
Owner

Original author: pavel.finkelshtein (February 24, 2012 21:47:31)

What steps will reproduce the problem?
1.create file torrent.json
with contents
{"build":25053, "props":[
{
"hash":"71D6FC08482AEF2903C5994C4842455D5C97CD63",
"trackers":"http://bt.rutracker.org/ann?uk=11111111\r\n\r\nhttp://retracker.local/announce\r\n\r\nhttp://ix.rutracker.net/ann?uk=111111111\r\n
"ulrate":0,
"dlrate":0,
"superseed":0,
"dht":1,
"pex":1,
"seed_override":0,
"seed_ratio":1500,
"seed_time":0
}
]}

  1. try to generate POJO

What is the expected output?
Some POJO

What do you see instead?
Instead I see emty directory "java-gen"

What version of the product are you using? On what operating system?
I'm using jsonschema2pojo-cli-0.2.1.jar
Ubuntu 11.10 x64
java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)

Original issue: http://code.google.com/p/jsonschema2pojo/issues/detail?id=42

@joelittlejohn
Copy link
Owner Author

From joelittl...@gmail.com on February 24, 2012 21:55:31
I'm afraid your torrent.json isn't a valid JSON Schema document. Right now jsonschema2pojo doesn't validate the incoming schema so you don't get any error message (we're waiting for a good Java implementation of JSON Schema, see issue # 1).

This project operates on JSON Schema documents (http://json-schema.org). It should be pretty easy to write one for your case. It would look something like:

{
"type" : "object",
"properties" : {
"build" : { "type" : "integer"},
"props" : {
"type" : "object",
"properties" : {
"hash" : { "type" : "string" },
"trackers" : { "type" : "string" },
"ulrate" : { "type" : "integer" },
...
}
}
}

@joelittlejohn
Copy link
Owner Author

From pavel.finkelshtein on February 25, 2012 05:48:32
OK, it makes a perfect sense. But mb it's possiblie to understand that "something" is of type "String", true is of type "Boolean", and 23 is of type Number? Just like here:
http://jsongen.byingtondesign.com/

@joelittlejohn
Copy link
Owner Author

From joelittl...@gmail.com on February 25, 2012 22:52:25
Thanks Pavel. It may be worth asking the author of jsongen (http://www.byingtondesign.com) if he could be convinced to open source his tool and offer it as a library or maven plugin.

As you say though, I'm sure it would be quite simple to implement such a tool that could generate POJOs by example (given example json) rather than schema. That's not really the goal of this project but it's something that I might have a play with at some point.

I think we can turn this ticket into an enhancement request. If there's enough interest then we could look at including support for this kind of input into jsonschema2pojo.

@joelittlejohn
Copy link
Owner Author

From pavel.finkelshtein on February 26, 2012 05:47:06
OK, just let me try to explain, why this can be important. A lot of JSON RPC API's work with such kind of JSON, example above is from uTorrent, and, as I remember, GWT works with similar requets in their GWT RPC, and, I think, there are more examples...

Thank you for your interest!

@joelittlejohn
Copy link
Owner Author

From joelittl...@gmail.com on August 28, 2012 22:25:18
This is fixed in master ready for 0.3.3 (re24d361eabf7). This is a straw-man implementation but it covers the most useful constructs I think and appears to work pretty well.

In 0.3.3 documentation for Maven/Ant/CLI will describe a new config property sourceType that can take the value JSONSCHEMA (default) or JSON. Using sourceType=JSON will activate this new behaviour and source files will be assumed to contain example json.

@joelittlejohn
Copy link
Owner Author

From pavel.finkelshtein on December 28, 2012 11:47:07
Thank you so much!

I'm again on way to write new uTorrent client :)
JSON works almost perfect (the last thing i need is conversion of inner arrays to objects, but I think it couldn't be easily achieved.

Thank you again!

Best,
Paul.

@joelittlejohn
Copy link
Owner Author

From joelittl...@gmail.com on January 02, 2013 09:46:05
Paul, could you explain a bit more about what you mean by 'conversion of inner arrays to objects'?

If you could provide an example then we could discuss some options.

@joelittlejohn
Copy link
Owner Author

From pavel.finkelshtein on January 02, 2013 12:57:34
Of course. From same API from which was initial request I can get JSON like this:

{"build":27079,"files": ["18A6780C343EB9AA52457E875AC750EB4772C952",
[["Stargate.Atlantis.S01E01-02.Rising.DVDRip.XviD.Rus.avi",786382346,786382346,2,0,188,true,155104,5070,624,352,-1,-5469654900294025217]
,["Stargate.Atlantis.S01E03.Hide And Seek.DVDRip.XviD.Rus.avi",390565362,390565362,2,187,94,true,155109,2518,624,352,-1,-5469654556696641537]
,["Stargate.Atlantis.S01E18.The.Gift.DVDRip.XviD.Rus.avi",390175898,390175898,0,280,94,true,155139,2515,624,352,-1,-5469654213099257857]
,["Stargate.Atlantis.S01E07.Poisoning.The.Well.DVDRip.XviD.Rus.avi",390101900,390101900,0,373,94,true,155110,2515,624,352,-1,-5469653869501874177]
,["Stargate.Atlantis.S01E06.Childhood's.End.DVDRip.XviD.Rus.avi",390099610,390099610,0,466,94,true,0,0,0,0,-1,-5469653525904490497]
,["Stargate.Atlantis.S01E05.Suspicion.DVDRip.XviD.Rus.avi",390099210,390099210,2,559,94,true,0,0,0,0,-1,-5469653182307106817]
,["Stargate.Atlantis.S01E04.Thirty-Eight.Minutes.DVDRip.XviD.Rus.avi",390097788,390097788,2,652,94,true,0,0,0,0,-1,-5469652838709723137]
,["Stargate.Atlantis.S01E10.The.Storm.DVDRip.XviD.Rus.avi",390097712,390097712,2,745,94,true,155108,2515,624,352,-1,-5469652495112339457]
,["Stargate.Atlantis.S01E09.Home.DVDRip.XviD.Rus.avi",390094086,390094086,1,838,94,true,0,0,0,0,-1,-5469652151514955777]
,["Stargate.Atlantis.S01E14.Sanctuary.DVDRip.XviD.avi",389946334,389946334,2,931,94,true,155109,2514,624,352,-1,-5469651807917572097]
,["Stargate.Atlantis.S01E13.Hot.Zone.DVDRip.XviD.avi",389945932,389945932,2,1024,94,true,155109,2514,624,352,-1,-5469651464320188417]
,["Stargate.Atlantis.S01E19.The.Siege.(Part 1).DVDRip.XviD.Rus.avi",389945828,389945828,2,1117,94,true,155109,2514,624,352,-1,-5469651120722804737]
,["Stargate.Atlantis.S01E12.The.Defiant.One.DVDRip.XviD.Rus.avi",389944590,389944590,2,1210,94,true,155109,2514,624,352,-1,-5469650777125421057]
,["Stargate.Atlantis.S01E20.The.Siege.(Part 2).DVDRip.XviD.Rus.avi",389944482,389944482,2,1303,94,true,155109,2514,624,352,-1,-5469650433528037377]
,["Stargate.Atlantis.S01E15.Before.I.Sleep.DVDRip.XviD.Rus.avi",389942368,389942368,2,1396,94,true,155108,2514,624,352,-1,-5469650089930653697]
,["Stargate.Atlantis.S01E16.The.Brotherhood.DVDRip.XviD.Rus.avi",389634444,389634444,2,1489,94,true,155109,2512,624,352,-1,-5469649746333270017]
,["Stargate.Atlantis.S01E08.Underground.DVDRip.XviD.Rus.avi",389171668,389171668,2,1582,94,true,0,0,0,0,-1,-5469649402735886337]
,["Stargate.Atlantis.S01E17.Letters.From.Pegasus.DVDRip.XviD.Rus.avi",389095262,389095262,2,1675,93,true,154771,2514,624,352,-1,-5469649059138502657]
,["Stargate.Atlantis.S01E11.The.Eye.DVDRip.XviD.Rus.avi",386687100,386687100,2,1767,94,true,155109,2493,624,352,-1,-5469648715541118977]
]
]
}

Here you can see example of really bad API, because things which are objects really are represented as Arrays, but I can't change it, so I should work with what I get.
I just thought if it's possible to add some config option, that will represent arrays as objects with field names like field1,field2...
Possibly it's impossible in common case;

The only thing that may be possible is for every array-object create class, extending some List implementation (e.g. ArrayList) with overriden modification functions - add, addAll and so on. Other way is to implement own realization of List, but I really think that any of this ways will be pain for you.

@joelittlejohn
Copy link
Owner Author

From joelittl...@gmail.com on January 03, 2013 09:49:54
I think you're right, there's probably no good way to generalise this into something that's useful to add to the plugin. As you say, I think this is a case of poor JSON structure and not something that is worth supporting in jsonschema2pojo.

@joelittlejohn
Copy link
Owner Author

From pavel.finkelshtein on January 03, 2013 10:08:40
BTW, don't you know - is there any way to convert such array to object?
I mean direct manipulation with JSON string. Maybe some useful regexps or things like it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant