0

I'm looking to dump, rather toString() a section of JSON that I want to store locally as a string, since it it is highly variable and not imperative for me to know the contents.

I'm using JsonReader for the parsing: https://developer.android.com/reference/android/util/JsonReader.html

Unfortunately, when I reach the token that contains what I want to dump, JsonReader does not have a method for dumping the entire JSON to a string.

This is the closest that it has: https://developer.android.com/reference/android/util/JsonReader.html#toString()

It seems that I may have to use regex to pull out the value of the token I am targeting.

Is there another, better solution? How would I do this with regex?

Assume that this is the sample JSON and I am targeting the user key:

{
   "id": 912345678901,
   "text": "How do I read JSON on Android?",
   "geo": null,
   "user": {
      "name": "android_newb",
      "followers_count": 41
   }
}
lewicki
  • 480
  • 8
  • 21
  • I once asked this question and got no reasonable responses: https://stackoverflow.com/questions/47289626/preserving-parts-of-raw-content-during-json-and-or-xml-deserialisation I am still using `toString()`, which works, but then again first parsed and then converted back to string – Oleg Sklyar Mar 12 '18 at 14:40
  • @OlegSklyar ugh – lewicki Mar 12 '18 at 14:51
  • If you find a solution, I'd be most interested to learn about it :) And yes, I tried both fasterxml ObjectMapper and Gson – Oleg Sklyar Mar 12 '18 at 14:55
  • I just looked into extending JsonReader but the class is `final`. Go figure. – lewicki Mar 12 '18 at 15:00
  • I think I'm going to add a stringified sibling from my server of the objects I want to dump. I hate it, but I need to keep moving. – lewicki Mar 12 '18 at 15:09
  • @OlegSklyar https://stackoverflow.com/a/24085100/255543 – lewicki Mar 12 '18 at 15:19
  • But that is exactly 1:1 my solution and it has exactly the same issue: by the time the decerializer is called JSON is already parsed (it is already a tree), which is then dumped back as JSON. – Oleg Sklyar Mar 12 '18 at 15:50

0 Answers0