Click here to Skip to main content
15,888,521 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: To get a %age divide the number by the number of decimal places defined on the column Pin
Nelek13-Dec-19 3:04
protectorNelek13-Dec-19 3:04 
GeneralRe: To get a %age divide the number by the number of decimal places defined on the column Pin
Marc Clifton13-Dec-19 4:05
mvaMarc Clifton13-Dec-19 4:05 
GeneralRe: To get a %age divide the number by the number of decimal places defined on the column Pin
Nelek13-Dec-19 4:11
protectorNelek13-Dec-19 4:11 
GeneralRe: To get a %age divide the number by the number of decimal places defined on the column Pin
CHill6013-Dec-19 5:07
mveCHill6013-Dec-19 5:07 
GeneralRe: To get a %age divide the number by the number of decimal places defined on the column Pin
Nelek13-Dec-19 3:08
protectorNelek13-Dec-19 3:08 
GeneralRe: To get a %age divide the number by the number of decimal places defined on the column Pin
Tiger1250619-Dec-19 15:33
Tiger1250619-Dec-19 15:33 
GeneralRe: To get a %age divide the number by the number of decimal places defined on the column Pin
CHill6019-Dec-19 21:08
mveCHill6019-Dec-19 21:08 
GeneralDev time waster ala JSON dialect PinPopular
raddevus9-Dec-19 10:24
mvaraddevus9-Dec-19 10:24 
I have an app that runs on multiple (Windows, iOS, Android - as both a native app or as a web app).
JSON is a nice lingua franca -- I thought.

You see my app has simple string-based keys the user adds to keep track of her sites.
Since I'm not sure what the user might type, I go ahead and encode the keys to Base64.

Normally you see some Base64 encoded keys which look like:
c3VwZXJzaXRl
c2Vjb25kU2l0ZQ==
dGhyZWU=

Serialize Object As JSON
In my web apps and on Windows I serialize all the user's sites as objects via JSON and it works great and looks like:
[{"HasSpecialChars":false,"HasUpperCase":false,"Key":"c3VwZXJzaXRl","MaxLength":0},{"HasSpecialChars":false,"HasUpperCase":false,"Key":"dGhyZWU=","MaxLength":0},{"HasSpecialChars":false,"HasUpperCase":false,"Key":"c2Vjb25kU2l0ZQ==","MaxLength":0},{"HasSpecialChars":false,"HasUpperCase":false,"Key":"eWV0QW5vdGhlcg==","MaxLength":0}]

Of course on web sites (JavaScript), its the old JSON.stringify(allSiteKeys) that handles that so nicely.
And on Windows I've always used NewtonSoft libraries and it all works great. All interchangeable.

Android Gson - Google's JSON
Enter the problem.
However, while developing on Android I wanted to serialize the data the same way so I turned to Gson which is Google's official Android way of doing this.
However, I noticed that values which were Base64 encoded properly were output to JSON in an interesting way. Every equal sign was altered to \u003d which is the unicode equals sign[^].
That means my JSON would be altered to look like:
[{"HasSpecialChars":false,"HasUpperCase":false,"Key":"c3VwZXJzaXRl","MaxLength":0},{"HasSpecialChars":false,"HasUpperCase":false,"Key":"dGhyZWU\u003d","MaxLength":0},{"HasSpecialChars":false,"HasUpperCase":false,"Key":"c2Vjb25kU2l0ZQ\u003d\u003d","MaxLength":0},{"HasSpecialChars":false,"HasUpperCase":false,"Key":"eWV0QW5vdGhlcg\u003d\u003d","MaxLength":0}]

NOTE: This is not a case of oddly encoded Base64 -- if I look directly at the Base64 encoded data it still has the equals signs. This only occurred when the data was serialized to JSON.

Searching For An Answer
It wasn't easy to find the answer, because a Google developer explains it this way[^]:
Google dev
= is a special javascript character that must be escaped to unicode in
JSON so that a string literal can be embedded in XHTML without further
escaping.


XHTML? Oh, are we back in the year 2000? Roll eyes | :rolleyes:

It made no sense to me and others who were like, "Uh, JSON is a format for transmission. Why would I care if there is an equals sign in the data? It's just bytes."

It is quite difficult to find docs on the Gson library. Not great stuff.
But, finally, I found a stackoverflow answer[^] that mentions that you have to use the Gson builder to create the initial Gson object, and when you do you have to turn off html escaping...
Java
Gson gson = new GsonBuilder().disableHtmlEscaping().create();

Normally, that would just looke like:
Java
Gson gson = new Gson();


Everything else is the same so when you serialize it you still call the same code:
Java
String jsonSiteKeys = gson.toJson(allSiteKeys);


The GsonBuilder() is almost like magic, because how would you ever know it is there?

Different Dialect Which Includes HTML Escaping
The final point is "Why would the dev think that the normal case is to include HTML escaping when this is a transmit format?" Why not think that is the special case? Especially since other libraries handle it without the escaping.

All That For Compatible JSON?
That is a sick amount of time just to get JSON in a compatible format.
In the future when AI takes over all development work, how will it handle this? It will not allow anyone else to write JSON libraries. Roll eyes | :rolleyes:

modified 9-Dec-19 17:03pm.

GeneralRe: Dev time waster ala JSON dialect Pin
Jon McKee9-Dec-19 13:13
professionalJon McKee9-Dec-19 13:13 
GeneralRe: Dev time waster ala JSON dialect Pin
raddevus10-Dec-19 3:32
mvaraddevus10-Dec-19 3:32 
GeneralRe: Dev time waster ala JSON dialect Pin
kmoorevs10-Dec-19 6:36
kmoorevs10-Dec-19 6:36 
GeneralRe: Dev time waster ala JSON dialect Pin
raddevus11-Dec-19 2:40
mvaraddevus11-Dec-19 2:40 
GeneralRe: Dev time waster ala JSON dialect Pin
Sander Rossel11-Dec-19 2:10
professionalSander Rossel11-Dec-19 2:10 
GeneralRe: Dev time waster ala JSON dialect Pin
raddevus11-Dec-19 2:46
mvaraddevus11-Dec-19 2:46 
GeneralRe: Dev time waster ala JSON dialect Pin
Sander Rossel11-Dec-19 3:42
professionalSander Rossel11-Dec-19 3:42 
GeneralRe: Dev time waster ala JSON dialect Pin
raddevus11-Dec-19 4:32
mvaraddevus11-Dec-19 4:32 
GeneralRe: Dev time waster ala JSON dialect Pin
Sander Rossel11-Dec-19 4:37
professionalSander Rossel11-Dec-19 4:37 
GeneralRe: Dev time waster ala JSON dialect Pin
F-ES Sitecore11-Dec-19 3:47
professionalF-ES Sitecore11-Dec-19 3:47 
GeneralRe: Dev time waster ala JSON dialect Pin
  Forogar  11-Dec-19 4:27
professional  Forogar  11-Dec-19 4:27 
GeneralRe: Dev time waster ala JSON dialect Pin
Sander Rossel11-Dec-19 4:39
professionalSander Rossel11-Dec-19 4:39 
GeneralRe: Dev time waster ala JSON dialect Pin
raddevus11-Dec-19 4:34
mvaraddevus11-Dec-19 4:34 
GeneralRe: Dev time waster ala JSON dialect Pin
Bernhard Hiller5-Jan-20 21:49
Bernhard Hiller5-Jan-20 21:49 
GeneralRe: Dev time waster ala JSON dialect Pin
englebart11-Dec-19 2:50
professionalenglebart11-Dec-19 2:50 
GeneralRe: Dev time waster ala JSON dialect Pin
mlportersr217-Dec-19 5:42
mlportersr217-Dec-19 5:42 
GeneralYARTH-JS : Yet Another Reason To Hate JavaScript PinPopular
raddevus29-Oct-19 8:45
mvaraddevus29-Oct-19 8:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.