B4J Question Can Escaping be disabled in JSONGenerator ?

aminoacid

Active Member
Licensed User
Longtime User
I have looked all over to find a way to disable the "escaping" of the slash "/" in a JSON string created by the JSONGenerator but I am at a loss at this point without a solution. For example in this very simple code fragment:


B4X:
  Dim Map1 As Map
    Map1.Initialize
    
    Map1.Put("AdminFolder","root/users/admin")
    
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Map1)
    Log(JSONGenerator.ToPrettyString(2))

The Log produces:

{ "AdminFolder": "root\/users\/admin" }

What I would like to get is:

{ "AdminFolder": "root/users/admin" }


Any suggestions would be greatly appreciated.
Thanks!
 

drgottjr

Expert
Licensed User
Longtime User
you could do it by hand (like you did), but it might not be readable on the other end. escaping certain characters is part of the json spec. it's like asking, when i translate something into italian, why do the words come out in italian? they're supposed to.
 
Upvote 0

Jorge M A

Well-Known Member
Licensed User
Having said that by @drgottjr, you may want to reconsider the structure of the json object, in case you are in control of the receiving side, with something like:

Any suggestions would be greatly appreciated.

{
"AdminFolder": {
"folder": "root",
"subfolder": "users",
"user": "admin"
}
}

B4X:
    Private JS As String
    Private root As Map
    root.Initialize
    root.Put("AdminFolder",CreateMap("folder": "root" , "subfolder": "users",  "user": "admin"))

    Private JSONGenerator As JSONGenerator
  
    JSONGenerator.Initialize(root)
  
    Log("JSON")
    JS = JSONGenerator.ToPrettyString(2)
    Log(JS)
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Thanks for the feedback. I do have a work-around, but I thought there may be a more direct way of disabling the "escaping" part of the JSON spec. This works:

B4X:
    Dim Map1 As Map
    Map1.Initialize
    Dim s As String
    
    Map1.Put("AdminFolder","root/users/admin")
    
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Map1)
    s = JSONGenerator.ToString
    s=s.Replace("\/","/")
    Log(s)

Result is:

{"AdminFolder":"root/users/admin"}
 
Upvote 0
Top