Giter Club home page Giter Club logo

jsontojava's Introduction

Json2Java

I was fed up with writing Java classes to mirror json models. So I wrote this Java app to automate the process.

What this tool can do right now:

Give it:
  • a url to a json file
  • a package name for the classes it will generate
  • the name of the base class to start with.
  • optionally, the p option will make your classes parcelable.
  • optionally, the g option will give your classes gson annotations for serialization.
  • optionally, the s option will override the toString method.

It will create the folder structure for the package you provide, then it will read the json at the url you provide, and output java classes into a zip file.

The easiest way to actuall use this is to go to http://jsontojava.appspot.com/
There you can just put in the variables and click download.

When run from the commandline, it looks like this:

java -jar JsonToJava.jar -url http://example.com/folder.json -package com.example.api.model -class Folder -p -g

Each class will implement Parcelable for easy passing of information in Android (optional). Each class will have a default empty constructor. The members of each class will follow Android naming conventions with an "m" prefix and camel case. Each member will have a corresponding static final String that relates it to it's json counterpart Each member will have a @SerializedName annotation for very easy Gson parsing. (optional) If a member called "mId" or "mUniqueId" is found, then equals and hashcode will be overridden so comparisons are made on the id.

What I want this tool to do in the future in no particular order

  • Accept either a url or a block of json pasted in a text box
  • Extract superclasses
  • Accept multiple urls to build up the whole model
  • Allow the user to choose between Gson annotations, Jackson annotations, org.json constructors and toJson() methods, or none of the above
  • Give users the ability to rename classes
  • Give users the ability to ignore classes

jsontojava's People

Contributors

jonfhancock avatar npike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsontojava's Issues

Uses this data, finally generates the result is not right.

{"liveInfo":[{"id":"info","des":"","audio":"http://live.3gv.hehe.com/101","bkgURL":"http://y0.heheimg.com/a/2014_10/e08b6b07cb74d3f.jpg","channelId":"4AC51C17-9FBE-47F2-8EE0-8285A66EAFF5","unVideo":"http://live.cuff.hehe.com/zixun.m3u8","name":"hehe","smallIconURL":"http://y1.heheimg.com/a/2014_10/ccddb083e872c7b.png","unAudio":"http://live.cuff.hehe.com/101","bigIconURL":"http://y3.heheimg.com/a/2014_10/3ceafc4fb3b0b00.png","video":"http://live.3gv.hehe.com/zixun.m3u8"},{"id":"chinese","des":"","audio":"http://live.3gv.hehe.com/102","bkgURL":"http://y2.heheimg.com/a/2014_10/4a5c63674d61418.jpg","channelId":"270DE943-3CDF-45E1-8445-9403F93E80C4","unVideo":"http://live.cuff.hehe.com/zhongwen.m3u8","name":"hehehuhu","smallIconURL":"http://y1.heheimg.com/a/2014_10/1ee94d0bfb238a9.png","unAudio":"http://live.cuff.hehe.com/102","bigIconURL":"http://y1.heheimg.com/a/2014_10/38cf06f306393ef.png","video":"http://live.3gv.hehe.com/zhongwen.m3u8"},{"id":"hongkong","des":"","audio":"http://live.3gv.hehe.com/103","bkgURL":"http://y3.heheimg.com/a/2014_10/1e65a127e23bc1a.jpg","channelId":"2c942450-2165-4750-80de-7dff9c224153","unVideo":"http://live.cuff.hehe.com/hongkong.m3u8","name":"hehexxxx","smallIconURL":"http://y3.heheimg.com/a/2014_10/477fb893c64278d.png","unAudio":"http://live.cuff.hehe.com/103","bigIconURL":"http://y3.heheimg.com/a/2014_10/0b7ae487eb72af1.png","video":"http://live.3gv.hehe.com/hongkong.m3u8"},{"id":"cctv13","des":"","audio":"http://live.3gv.hehe.com/161","bkgURL":"http://y0.heheimg.com/a/2014_10/58296d58098f3a4.jpg","channelId":"5435BFA3-210B-4F4F-A90F-BCB1C4C40D59","unVideo":"http://live.cuff.hehe.com/cctv13.m3u8","name":"CCTV13","smallIconURL":"http://y3.heheimg.com/a/2014_10/0cd0c207cf2dc10.png","unAudio":"http://live.cuff.hehe.com/161","bigIconURL":"http://y3.heheimg.com/a/2014_10/bec87305e93dba6.png","video":"http://live.3gv.hehe.com/cctv13.m3u8"},{"id":"ahws","des":"","audio":"http://live.3gv.hehe.com/165","bkgURL":"http://y0.heheimg.com/a/2014_10/7b8ff78bfc711d0.jpg","channelId":"8550DAD6-8724-4474-8A95-F30967CAA294","unVideo":"http://live.cuff.hehe.com/ZJWS.m3u8","name":"fdsfsfd","smallIconURL":"http://y1.heheimg.com/a/2014_10/177e597345de785.png","unAudio":"http://live.cuff.hehe.com/165","bigIconURL":"http://y0.heheimg.com/a/2014_10/a2bf9373fdb4a43.png","video":"http://live.3gv.hehe.com/ZJWS.m3u8"},{"id":"tjws","des":"","audio":"http://live.3gv.hehe.com/166","bkgURL":"http://y2.heheimg.com/a/2014_10/8b8965f5c5ebcfc.jpg","channelId":"35383695-26c3-4ce5-b535-0001abce11e4","unVideo":"http://live.cuff.hehe.com/CQWS.m3u8","name":"sdfsdfs","smallIconURL":"http://y3.heheimg.com/a/2014_10/54d1e174ec8d781.png","unAudio":"http://live.cuff.hehe.com/166","bigIconURL":"http://y1.heheimg.com/a/2014_10/f55c789dc554969.png","video":"http://live.3gv.hehe.com/CQWS.m3u8"},{"id":"szws","des":"","audio":"http://live.3gv.hehe.com/167","bkgURL":"http://y3.heheimg.com/a/2014_10/3c1a861ac668f1a.jpg","channelId":"3072FC61-4763-4BE9-AA19-2B241498775A","unVideo":"http://live.cuff.hehe.com/SZWS.m3u8","name":"sdfsfsf","smallIconURL":"http://y0.heheimg.com/a/2014_10/f2aa7a7b8cabf36.png","unAudio":"http://live.cuff.hehe.com/167","bigIconURL":"http://y0.heheimg.com/a/2014_10/eb2b80875e98698.png","video":"http://live.3gv.hehe.com/SZWS.m3u8"}]}

Implement toString()

It would be helpful if you could also generate the toString() method for each model class. It should output the values for each member variable. Here is an example:

@Override
public String toString() {
    return "deg = " + mDeg + ", gust = " + mGust + ", speed = " + mSpeed;
}

In the UI you can add an option to let the user decide whether the method should be generated.

NPE

Created PublicIpAddress.java
Created InnerIpAddress.java
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NullPointerException
    at com.jsontojava.Member.getGetterSignature(Member.java:104)
    at com.jsontojava.Member.getGetter(Member.java:125)
    at com.jsontojava.NewType.toString(NewType.java:128)
    at com.jsontojava.Main.main(Main.java:90)

code generation renames the reference variables, class names randomly

1>
{
"responseCode": "200",
"responseMessage": "OK"
}

private String mResponseMessage this should be responseCode. when we use GSON parsing it's really helpful.

2>
I also observed if the Json object prefix is { "Options" :{.....}} the class name changes from Options to Option.java !! it should be Options.java

3>
"doc_value": "a2" after code generation changes to String mDocValue; this is not good! we have to rename it again or we have to use @SerializedName("3") annotations!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.