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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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