This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
README
222 lines (164 loc) · 5.82 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
JSON 2 POJO
This script, json2pojo, attempts to create java POJOs from example JSON files.
In general, error handling is pretty woeful, so expect it to crash out with a
stacktrace if you do something bad. :)
For command line flags, use json2pojo.rb -h.
There are limits to what can be achieved with automated generation. This
script works on a specific subset of JSON.
For example, this will work:
**************************************************************
{
"foo" : "blah",
"baz" : 1,
"xyz" : 2.5
}
**************************************************************
Using the default configuration, the Example java class will be generated in the "output" directory:
**************************************************************
package com.example;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
/**
* Created by json2pojo
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Example {
private Double xyz;
private Long baz;
private String foo;
public Double getXyz() {
return xyz;
}
public void setXyz(Double xyz) {
this.xyz = xyz;
}
public Long getBaz() {
return baz;
}
public void setBaz(Long baz) {
this.baz = baz;
}
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
**************************************************************
This example will generate code, but it's not very useful:
**************************************************************
{
"exchange_rates" :
{
"AUD" : 1.234,
"USD" : 0.998,
"JPY" : 5.678
}
}
**************************************************************
ExchangeRates file in "output" directory:
**************************************************************
package com.example;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
/**
* Created by json2pojo
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class ExchangeRates {
private Double AUD;
private Double USD;
private Double JPY;
public Double getAUD() {
return AUD;
}
public void setAUD(Double AUD) {
this.AUD = AUD;
}
public Double getUSD() {
return USD;
}
public void setUSD(Double USD) {
this.USD = USD;
}
public Double getJPY() {
return JPY;
}
public void setJPY(Double JPY) {
this.JPY = JPY;
}
}
**************************************************************
This script can handle simple arrays of primitives. It looks at the first entry only
to generate the list in Java. The following will generate a field as
"private List<Double> simpleNumbers;", etc.
**************************************************************
{
"simple_numbers" :
[
1.234,
0.998,
5.678
]
}
**************************************************************
The script can handle arrays that contain objects. It looks at the first entry only
to generate array objects. The following works:
**************************************************************
{
"simple_data" :
[
{ "key" : 1.234 },
{ "key" : 3.456 },
{ "key" : 7 }
]
}
**************************************************************
Note that if the last value ("key" : 7") was first, then the code generated would be
using Long instead of Double.
Also, by default, the script will chop the last character off array/list names. In
the above example, the default code generation would create a SimpleDat.java file.
Use -n to override. If your fields use "nice" plurals, then the default behaviour
generates nice java files.
Using this example:
**************************************************************
{
"links": [
{"href":"/example/2342342", "rel":"example", "group":"public"}
]
}
**************************************************************
The files generated by default will be Example.java and Link.java. The Example
class will contain a List<Link> which is more readable for java purposes.
If json2pojo can't determine a java type, it will use Object as the class type by
default. Use "-u" to override this behaviour and have unknown fields be given a
java class of "UNKNOWN", which will then cause the code to not compile and show
obvious errors for you to fix.
If your data contains fields that are java reserved words, you'll need to use
one or more of the flags "-fp", "-fs", "-cp", "-cs" to add a prefix or suffix
to field names and class names. Best results for these flags are to use the
prefix versions ("-fp" and "-cp"), but the suffix versions have been added
for convenience. Limitation: This does not handle fields or classes in your
data that are specifically named "boolean", "double", "long" or "string".
Example:
**************************************************************
json2pojo.rb -cp Foo -fp foo -e example_with_reserved_words.json
**************************************************************
Note in the above example the use of prefixes for classes ("-cp") and
fields ("-fp") and the different values. If you need to override the
class names, it's highly likely you will need to override the field
names as well.
By default, the generated classes are annotated for use with Jackson (see
http://jackson.codehaus.org/). Refer to the Configuration in the json2pojo.rb
file to change this if required. If you want strict JSON parsing in your java
application, then clear these properties in the Configuration:
...
self.ignore_unknown_properties_annotation = "@JsonIgnoreProperties(ignoreUnknown = true)"
self.ignore_unknown_properties_import = "import org.codehaus.jackson.annotate.JsonIgnoreProperties;"
...
Note that if you use the flags "-fp" and "-fs" to modify the field names, all fields
generated will be annotated with the @JsonProperty for the real field name within the
JSON.
Finally, feel free to send patches, fixes or suggestions for improving this script.
The code is hack-ish, so apologies for that. :)
Chris
chrisr AT rymich.com