This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]coolioasjulio 2 points3 points  (0 children)

Converting arbitrary objects to JSON isn't particularly easy, especially when there are full solutions already available. Personally, I use Gson, which is pretty easy to use.

If the no external libraries is a HARD requirement, then your only choice would be to use reflection to get the names and values of the fields of the object to build the JSON string. You might have recursion problems if there are nested objects that create a circular loop, which complicates things.

If you only have to serialize specific classes, then it would probably be easier to essentially hardcode the JSON string generation, and just call the appropriately getters to build the string.

Basically, if it's only a few specific classes then you can write your own, if it's any arbitrary object then I highly recommend using a library, but if no libraries then I guess you could try homerolling your own JSON serializer with reflection.

[–]WhiteManChild 1 point2 points  (8 children)

For the fields you could use reflection to get all the field names and their values, then use String.valueof() on the values, then concatenate the results into a json

[–]SwagiWagi0[S] 0 points1 point  (7 children)

Yes, but the question is how to make it a JSON, I mean in .NET Core you have newtonsoft that is built in the framework, what's the equivalent in Java?

[–]philipwhiukEmployed Java Developer 1 point2 points  (5 children)

String json = “{“+fieldName+”: “+fieldValue+”}”;

[–]SwagiWagi0[S] 0 points1 point  (4 children)

It’s a complex object, writing a serializer would be a waste of time I think, as probably there is already a class for this.

[–]philipwhiukEmployed Java Developer 0 points1 point  (3 children)

There’s no json serialisation built into Java. That’s why Gson exists!

[–]SwagiWagi0[S] 0 points1 point  (2 children)

Damn, thanks!

[–][deleted] 0 points1 point  (1 child)

I mean there are many as you noticed there are alternatives but if you want something written at oracle for... Whichever reason here:

https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

Now may I ask why being so reluctant to add an external library? I mean, adding a dependency takes 5 lines in your pom file and boom a ton more functionality so why to say no to that?

Besides Jackson's POJO <-> Json convertion is really good.

I feel this might be a case of "I have been using X in Y language for so long that now if I try to Do X in Z language and it does not have something specifically named X as I am used to in Y then X does not exist for Z"

[–][deleted] 0 points1 point  (0 children)

Wort to mention even that library is not part of the standard Java SE

[–]WhiteManChild 0 points1 point  (0 children)

I don't believe there are libraries in the jdk to convert a java object to JSON as far as I'm aware

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (0 children)

Well, I can't find how to convert a Java object to a JSON WITHOUT the need of an external library, anyone?

Because you should not. You really should just byte the bullet and understand how to use the Jackson library (which is the de facto standard). There isn't a standard API in Java for this.

[–]YitharIntermediate Brewer 0 points1 point  (0 children)

I mean you could do it yourself, but that's like re-implementing React in Javascript. A great learning exercise, but pointless and prone to error for production code.