I am starting to create a Samsung TV App, which runs a pretty tight system where I can barely work with more than JS and HTML5. Furthermore, I have to send a json request to my web server to get the data down to the emulator client. For easier representation of my data I wanted to covert the json into XML. But due to having utf-8 encoded data on the web, it seems impossible for me to automate the decoding from json and encoding into XML.
Not like in some of the posts and aritcles I read, the json_encode creates HTML entities except of representations as something like \uXXXXXX.
So, what can I do?
the json_encode creates HTML entities
No, it doesn't.
except of representations as something like \uXXXXXX.
Those are JSON unicode escapes
So, what can I do?
Parse the JSON as normal. Generate the XML as normal.
Any JSON library and any XML library should be able to handle unicode characters.
Related
In Python, when sending a POST request to an API endpoint, we need to encode() the data, that encodes it using UTF-8 and bytes type is assigned to the resulting Object. Why exactly do we need to encode it before sending?
What if we send it without encoding it? What would happen if we send it as a Unicode string instead of converting it to UTF-8?
In JavaScript, we do something similar like JSON.stringify(). Does this serve the same purpose as encode()? Why exactly do we need to use stringify() in a POST request?
I know the overall working of character encodings, but I cannot find any article that explains it deeply as to why exactly we use JSON.stringify()/JSON.parse() in JS and encode()/decode() in Python. Can we not send data without using these functions? Can anyone explain by comparing these four functions?
I'm building a web app, and need to be able to encode user-generated data as concisely as possible before transmitting it to my server. In the past I've used Flash, and it had a very neat system where for any class that you want to serialize, you could write a pair of functions that would describe exactly how to serialize the data. For example:
out.writeShort(session);
out.writeUnsignedInt(itemID);
out.writeObject(arbitraryData);
out.writeShort(score);
You would have to write an equivalent function to read bytes from the serialized data and build the class from it.
Once data is serialized it could be encoded into a Base64 string for safe network transmission to the server.
I can't figure out how to do this in Javascript? JSON is nice and easy but it's incredibly wasteful, sending all object key/value pairs, and unless I'm mistaken everything is encoded as a string? So the value false is encoded as the string "false"?
Any advice on how to implement this in Javascript would be greatly appreciated! Use of libraries is fine so long as they work both on Node and in browser.
Look at this answer. You can use BSON format (Binary JSON) and it doesn't have those features of JSON you mentioned.
I have been working with JSONP on my GWT application. When my server sends a json string, I can get it in the form of a JavaScriptObject on the client side.
My problem is my json has complicated structures: using maps, nests with a lot of different keys. That is a big pain to extract data (I may have to write few hundred functions for all keys to extract data one by one and some complicated codes to fill maps).
I am considering few solutions:
Encode and send whole json strings as normal strings to client (as a value of a simple json string). Just worry my encoded strings may be few time longer than the original ones and may easily exceed the limit of 2k long
Convert back a JavaScriptObject into a pure string (similar to one I sent from the server)
After having a pure string I will parse it using some json parsers / methods to the structures I feel convenient.
My questions:
1) How to convert back a JavaScriptObject object into a pure / original json string?
2) Any idea about solutions?
Many thanks
1) Convert JavaScriptObject to JSON: JsonUtils.stringify(yourJSO)
Convert JSON to JavaScriptObject: JsonUtils.safeEval(jsonString);
2) Did you think about using AutoBeans?? Check out the GWT page
How to decode cp-1251 to UTF-8 in javascript?
The cp-1251 is from a datafeed, which required to decode from js client side.
There is no way to change server side output, since it is related to a 3rd party, and due to some reason, I would not use any server side programming to convert the datafeed to become another datafeed.
(Assuming that by "UTF-8" you meant the JS strings in their native encoding...)
Depending on the format your 'cp-1251' data is in and depending on the browsers you need to support, you can choose from:
TextDecoder.decode() API (decodes a sequence of octets from a typed array, like Uint8Array) - if you're using web sockets, you can get an ArrayBuffer out of it to decode.
https://github.com/mathiasbynens/windows-1251 operates on something it calls 'byte strings' (JS Strings consisting of characters like \u00XY, where 0xXY is the encoded byte.
build the decoding table yourself (example)
Note that in most cases (not something as low-level as websockets though) it might be easier to read the data in the correct encoding before it ends up as a JS string (for example, you can force XMLHttpRequest to use a certain encoding even if the server misreports the encoding).
I need to get some XML, convert it to javascript, muck around with it, and convert it back to XML. I'm working in Node.js.
Currently, if I round trip it from XML to Javascript objects, then back to XML, I end up with a bunch of characters that are screwy (an apostrophe might look like this: ’ , for example), and CDATA sections are lost.
I've been using xml2js module (also tried xml2js-expat) to read the xml. The xml labels itself as being UTF-8, and I specify this to xml2js.
To convert back to XML, I am using this: http://goessner.net/download/prj/jsonxml/json2xml.js
But it appears the problem is with the first step.
Any solutions to this?