Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

built in parser for json?

 
Ranch Foreman
Posts: 117
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I'm currently playing around with some simple json and its parsing into a collection structure - and just did my usual System.out.println() as debug - and noticed the output to be very similar to the JSON input.
Start of original JSON input:

Output of my System.out.println(List<Map<String, String>>) (new lines added to avoid extreme long line - it's actually one long string without line breaks):

So, the output of List.toString() combined with Map.toString() pretty much resembles the JSON input - although all keys and values missing their quotes and the colon is replaced with an equals - but aside from these minor details which can be done with simple String.replace(String, String) the question came up in my head:
If the toString() methods are pretty much output almost-JSON'ish strings - are there any built in parsers doing the same in reverse without the need for use a JSON-lib or some hacky RegEx?
From earlier experiments with OAuth I know that RegEx isn't the best to parse JSON - although it's sufficient for "simple" structures as OAuth or this simple file list I'm dealing with.

Has anyone else already noticed this behaviour and maybe know more about this?

As a side-node: If someone is to suggest using JSON-lib to me: No issues doing that - but I would appreciate using a slim one without a lots of dependencies, as some of linked ones on the json.org site require.


// edit-notice: just fixed some typos
 
Sheriff
Posts: 22208
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though the output of a list's or map's toString looks like JSON, I never would use any string manipulation to turn it into proper JSON. You can't just replace = with :, add quotes, etc. What if your keys or values contain quotes, = signs, commas, etc? For similar reasons you should never use regular expressions for parsing complex structures like JSON, XML or HTML. Regular expressions simply can't handle those structures - the nesting alone is going to be impossible to handle properly.

That said: there are no built-in parsers for JSON. There are for XML, in the java.xml module, but not JSON. However, there are plenty of good libraries available. The ones I've used:
  • Jackson
  • Gson
  • JSON-P, a.k.a. JSR-374. Moved to Jakarta EE. I've used the GlassFish reference implementation


  • There are several others, but I can't remember ever having worked with any of those.
     
    Matthew Bendford
    Ranch Foreman
    Posts: 117
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for your reply.
    As for a few reasons it doesn't really matter any longer, but as for the libs you linked only googles Gson seems the one to go, as I struggle to find a way to download jackson and json-p seems EE stuff.
     
    Rob Spoor
    Sheriff
    Posts: 22208
    117
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    JSON-P is part of JEE, but you can also use it in JSE applications. The same goes for several JEE APIs. JPA for instance is well-known to be work in JSE.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic