Skip to content

Commit 7cc178b

Browse files
author
synapticloop
committed
updated JSON handling
1 parent a5c8acd commit 7cc178b

File tree

7 files changed

+263
-58
lines changed

7 files changed

+263
-58
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ plugins {
2121
group = 'synapticloop'
2222
archivesBaseName = 'h2zero'
2323
description = """lightweight ORM generator for mysql/sqlite, java with extensions for taglibs and routemaster"""
24-
version = '4.2.3'
24+
version = '4.2.4'
2525

2626
tasks.withType(Javadoc).all { enabled = false }
2727

src/main/resources/java-create-model.templar

+38-13
Original file line numberDiff line numberDiff line change
@@ -610,22 +610,47 @@ public class {table.javaClassName} {if !table.isConstant}extends ModelBase {endi
610610
{\t}}{\n}
611611

612612

613+
{-- and the getJSONString - for chaining --}
614+
{\t}public JSONObject getToJSON() {{{\n}
615+
{\t}{\t}return(toJSON());{\n}
616+
{\t}}{\n}
617+
613618
{-- Now we need the toJsonString Method--}
614619
{\n}
615-
{\t}public String toJsonString() {{{\n}
620+
{\t}public JSONObject toJSON() {{{\n}
621+
{\t}{\t}String nullString = null;{\n}{\n}
622+
623+
{\t}{\t}JSONObject jsonObject = new JSONObject();{\n}
624+
{\t}{\t}jsonObject.put("type", "{table.javaClassName}");{\n}
625+
{loop table.fields as field}
626+
{\t}{\t}if(null == this.{field.javaName}) {{{\n}
627+
{\t}{\t}{\t}jsonObject.put("{field.javaName}", nullString);{\n}
628+
{\t}{\t}} else {{{\n}
629+
{\t}{\t}{\t}jsonObject.put("{field.javaName}", this.{field.javaName});{\n}
630+
{\t}{\t}}{\n}
631+
{endloop}
632+
{\t}{\t}return(jsonObject);{\n}
633+
{\t}}{\n}
634+
{\n}
616635

617-
{\t}{\t}JSONObject jsonObject = new JSONObject();{\n}
618-
{\t}{\t}jsonObject.put("type", "{table.javaClassName}");{\n}
619-
{loop table.fields as field}
620-
{\t}{\t}if(null == this.{field.javaName}) {{{\n}
621-
{\t}{\t}{\t}jsonObject.putOpt("{field.javaName}", null);{\n}
622-
{\t}{\t}} else {{{\n}
623-
{\t}{\t}{\t}jsonObject.put("{field.javaName}", this.{field.javaName}.toString());{\n}
624-
{\t}{\t}}{\n}
625-
{endloop}
626-
{\t}{\t}return(jsonObject.toString());{\n}
627-
{\t}}{\n}
628-
{\n}
636+
637+
{-- Now we need the toJsonString Method--}
638+
{\n}
639+
{\t}public String toJsonString() {{{\n}
640+
{\t}{\t}String nullString = null;{\n}{\n}
641+
642+
{\t}{\t}JSONObject jsonObject = new JSONObject();{\n}
643+
{\t}{\t}jsonObject.put("type", "{table.javaClassName}");{\n}
644+
{loop table.fields as field}
645+
{\t}{\t}if(null == this.{field.javaName}) {{{\n}
646+
{\t}{\t}{\t}jsonObject.put("{field.javaName}", nullString);{\n}
647+
{\t}{\t}} else {{{\n}
648+
{\t}{\t}{\t}jsonObject.put("{field.javaName}", this.{field.javaName});{\n}
649+
{\t}{\t}}{\n}
650+
{endloop}
651+
{\t}{\t}return(jsonObject.toString());{\n}
652+
{\t}}{\n}
653+
{\n}
629654

630655
{-- and the getJSONString - for chaining --}
631656
{\t}public String getJsonString() {{{\n}

src/test/java/synapticloop/sample/h2zero/mysql/model/Pet.java

+56-12
Original file line numberDiff line numberDiff line change
@@ -228,39 +228,83 @@ public String toString() {
228228
stringBuilder.append(" Field[imgPhoto:" + this.imgPhoto + "]\n");
229229
return(stringBuilder.toString());
230230
}
231+
public JSONObject getToJSON() {
232+
return(toJSON());
233+
}
234+
235+
public JSONObject toJSON() {
236+
String nullString = null;
237+
238+
JSONObject jsonObject = new JSONObject();
239+
jsonObject.put("type", "Pet");
240+
if(null == this.idPet) {
241+
jsonObject.put("idPet", nullString);
242+
} else {
243+
jsonObject.put("idPet", this.idPet);
244+
}
245+
if(null == this.nmPet) {
246+
jsonObject.put("nmPet", nullString);
247+
} else {
248+
jsonObject.put("nmPet", this.nmPet);
249+
}
250+
if(null == this.numAge) {
251+
jsonObject.put("numAge", nullString);
252+
} else {
253+
jsonObject.put("numAge", this.numAge);
254+
}
255+
if(null == this.fltWeight) {
256+
jsonObject.put("fltWeight", nullString);
257+
} else {
258+
jsonObject.put("fltWeight", this.fltWeight);
259+
}
260+
if(null == this.dtBirthday) {
261+
jsonObject.put("dtBirthday", nullString);
262+
} else {
263+
jsonObject.put("dtBirthday", this.dtBirthday);
264+
}
265+
if(null == this.imgPhoto) {
266+
jsonObject.put("imgPhoto", nullString);
267+
} else {
268+
jsonObject.put("imgPhoto", this.imgPhoto);
269+
}
270+
return(jsonObject);
271+
}
272+
231273

232274
public String toJsonString() {
275+
String nullString = null;
276+
233277
JSONObject jsonObject = new JSONObject();
234278
jsonObject.put("type", "Pet");
235279
if(null == this.idPet) {
236-
jsonObject.putOpt("idPet", null);
280+
jsonObject.put("idPet", nullString);
237281
} else {
238-
jsonObject.put("idPet", this.idPet.toString());
282+
jsonObject.put("idPet", this.idPet);
239283
}
240284
if(null == this.nmPet) {
241-
jsonObject.putOpt("nmPet", null);
285+
jsonObject.put("nmPet", nullString);
242286
} else {
243-
jsonObject.put("nmPet", this.nmPet.toString());
287+
jsonObject.put("nmPet", this.nmPet);
244288
}
245289
if(null == this.numAge) {
246-
jsonObject.putOpt("numAge", null);
290+
jsonObject.put("numAge", nullString);
247291
} else {
248-
jsonObject.put("numAge", this.numAge.toString());
292+
jsonObject.put("numAge", this.numAge);
249293
}
250294
if(null == this.fltWeight) {
251-
jsonObject.putOpt("fltWeight", null);
295+
jsonObject.put("fltWeight", nullString);
252296
} else {
253-
jsonObject.put("fltWeight", this.fltWeight.toString());
297+
jsonObject.put("fltWeight", this.fltWeight);
254298
}
255299
if(null == this.dtBirthday) {
256-
jsonObject.putOpt("dtBirthday", null);
300+
jsonObject.put("dtBirthday", nullString);
257301
} else {
258-
jsonObject.put("dtBirthday", this.dtBirthday.toString());
302+
jsonObject.put("dtBirthday", this.dtBirthday);
259303
}
260304
if(null == this.imgPhoto) {
261-
jsonObject.putOpt("imgPhoto", null);
305+
jsonObject.put("imgPhoto", nullString);
262306
} else {
263-
jsonObject.put("imgPhoto", this.imgPhoto.toString());
307+
jsonObject.put("imgPhoto", this.imgPhoto);
264308
}
265309
return(jsonObject.toString());
266310
}

src/test/java/synapticloop/sample/h2zero/mysql/model/User.java

+70-16
Original file line numberDiff line numberDiff line change
@@ -284,49 +284,103 @@ public String toString() {
284284
stringBuilder.append(" Field[dtmSignup:" + this.dtmSignup + "]\n");
285285
return(stringBuilder.toString());
286286
}
287+
public JSONObject getToJSON() {
288+
return(toJSON());
289+
}
290+
291+
public JSONObject toJSON() {
292+
String nullString = null;
293+
294+
JSONObject jsonObject = new JSONObject();
295+
jsonObject.put("type", "User");
296+
if(null == this.idUser) {
297+
jsonObject.put("idUser", nullString);
298+
} else {
299+
jsonObject.put("idUser", this.idUser);
300+
}
301+
if(null == this.idUserType) {
302+
jsonObject.put("idUserType", nullString);
303+
} else {
304+
jsonObject.put("idUserType", this.idUserType);
305+
}
306+
if(null == this.flIsAlive) {
307+
jsonObject.put("flIsAlive", nullString);
308+
} else {
309+
jsonObject.put("flIsAlive", this.flIsAlive);
310+
}
311+
if(null == this.numAge) {
312+
jsonObject.put("numAge", nullString);
313+
} else {
314+
jsonObject.put("numAge", this.numAge);
315+
}
316+
if(null == this.nmUsername) {
317+
jsonObject.put("nmUsername", nullString);
318+
} else {
319+
jsonObject.put("nmUsername", this.nmUsername);
320+
}
321+
if(null == this.txtAddressEmail) {
322+
jsonObject.put("txtAddressEmail", nullString);
323+
} else {
324+
jsonObject.put("txtAddressEmail", this.txtAddressEmail);
325+
}
326+
if(null == this.txtPassword) {
327+
jsonObject.put("txtPassword", nullString);
328+
} else {
329+
jsonObject.put("txtPassword", this.txtPassword);
330+
}
331+
if(null == this.dtmSignup) {
332+
jsonObject.put("dtmSignup", nullString);
333+
} else {
334+
jsonObject.put("dtmSignup", this.dtmSignup);
335+
}
336+
return(jsonObject);
337+
}
338+
287339

288340
public String toJsonString() {
341+
String nullString = null;
342+
289343
JSONObject jsonObject = new JSONObject();
290344
jsonObject.put("type", "User");
291345
if(null == this.idUser) {
292-
jsonObject.putOpt("idUser", null);
346+
jsonObject.put("idUser", nullString);
293347
} else {
294-
jsonObject.put("idUser", this.idUser.toString());
348+
jsonObject.put("idUser", this.idUser);
295349
}
296350
if(null == this.idUserType) {
297-
jsonObject.putOpt("idUserType", null);
351+
jsonObject.put("idUserType", nullString);
298352
} else {
299-
jsonObject.put("idUserType", this.idUserType.toString());
353+
jsonObject.put("idUserType", this.idUserType);
300354
}
301355
if(null == this.flIsAlive) {
302-
jsonObject.putOpt("flIsAlive", null);
356+
jsonObject.put("flIsAlive", nullString);
303357
} else {
304-
jsonObject.put("flIsAlive", this.flIsAlive.toString());
358+
jsonObject.put("flIsAlive", this.flIsAlive);
305359
}
306360
if(null == this.numAge) {
307-
jsonObject.putOpt("numAge", null);
361+
jsonObject.put("numAge", nullString);
308362
} else {
309-
jsonObject.put("numAge", this.numAge.toString());
363+
jsonObject.put("numAge", this.numAge);
310364
}
311365
if(null == this.nmUsername) {
312-
jsonObject.putOpt("nmUsername", null);
366+
jsonObject.put("nmUsername", nullString);
313367
} else {
314-
jsonObject.put("nmUsername", this.nmUsername.toString());
368+
jsonObject.put("nmUsername", this.nmUsername);
315369
}
316370
if(null == this.txtAddressEmail) {
317-
jsonObject.putOpt("txtAddressEmail", null);
371+
jsonObject.put("txtAddressEmail", nullString);
318372
} else {
319-
jsonObject.put("txtAddressEmail", this.txtAddressEmail.toString());
373+
jsonObject.put("txtAddressEmail", this.txtAddressEmail);
320374
}
321375
if(null == this.txtPassword) {
322-
jsonObject.putOpt("txtPassword", null);
376+
jsonObject.put("txtPassword", nullString);
323377
} else {
324-
jsonObject.put("txtPassword", this.txtPassword.toString());
378+
jsonObject.put("txtPassword", this.txtPassword);
325379
}
326380
if(null == this.dtmSignup) {
327-
jsonObject.putOpt("dtmSignup", null);
381+
jsonObject.put("dtmSignup", nullString);
328382
} else {
329-
jsonObject.put("dtmSignup", this.dtmSignup.toString());
383+
jsonObject.put("dtmSignup", this.dtmSignup);
330384
}
331385
return(jsonObject.toString());
332386
}

src/test/java/synapticloop/sample/h2zero/mysql/model/UserPet.java

+35-6
Original file line numberDiff line numberDiff line change
@@ -213,24 +213,53 @@ public String toString() {
213213
stringBuilder.append(" Field[idPet:" + this.idPet + "]\n");
214214
return(stringBuilder.toString());
215215
}
216+
public JSONObject getToJSON() {
217+
return(toJSON());
218+
}
219+
220+
public JSONObject toJSON() {
221+
String nullString = null;
222+
223+
JSONObject jsonObject = new JSONObject();
224+
jsonObject.put("type", "UserPet");
225+
if(null == this.idUserPet) {
226+
jsonObject.put("idUserPet", nullString);
227+
} else {
228+
jsonObject.put("idUserPet", this.idUserPet);
229+
}
230+
if(null == this.idUser) {
231+
jsonObject.put("idUser", nullString);
232+
} else {
233+
jsonObject.put("idUser", this.idUser);
234+
}
235+
if(null == this.idPet) {
236+
jsonObject.put("idPet", nullString);
237+
} else {
238+
jsonObject.put("idPet", this.idPet);
239+
}
240+
return(jsonObject);
241+
}
242+
216243

217244
public String toJsonString() {
245+
String nullString = null;
246+
218247
JSONObject jsonObject = new JSONObject();
219248
jsonObject.put("type", "UserPet");
220249
if(null == this.idUserPet) {
221-
jsonObject.putOpt("idUserPet", null);
250+
jsonObject.put("idUserPet", nullString);
222251
} else {
223-
jsonObject.put("idUserPet", this.idUserPet.toString());
252+
jsonObject.put("idUserPet", this.idUserPet);
224253
}
225254
if(null == this.idUser) {
226-
jsonObject.putOpt("idUser", null);
255+
jsonObject.put("idUser", nullString);
227256
} else {
228-
jsonObject.put("idUser", this.idUser.toString());
257+
jsonObject.put("idUser", this.idUser);
229258
}
230259
if(null == this.idPet) {
231-
jsonObject.putOpt("idPet", null);
260+
jsonObject.put("idPet", nullString);
232261
} else {
233-
jsonObject.put("idPet", this.idPet.toString());
262+
jsonObject.put("idPet", this.idPet);
234263
}
235264
return(jsonObject.toString());
236265
}

0 commit comments

Comments
 (0)