Deserialize JSON containing an anonymous function using .net - json

I am getting a .net run-time error when attempting to desrialize a JSON string that contains an anonymous function:
Invalid JSON primitive: function.
JSON string itself looks like this:
{
Action: "fadeIn",
Callback: function(){doSomething();}
}
This makes me wonder if it is allowed to have anonymous functions in JSON strings that are to be serialized in .net. More specfically I can only use the .net frameworks's own JavaScriptSerializer class for deserialization. Can anyone confirm this, or have a solution?

JSON is a data representation protocol, and as such it can only be used to represent data, not behavior (which is what functions are). As your deserializer told you, what you have is not valid JSON (it's a valid JavaScript object, though, which causes some confusion). Check the JSON spec for more details on this format.
So to your question - no, you cannot deserialize JSON containing function, because it's not JSON in the first place.

Related

jsoncpp is having trouble reading my Gson output

I have a java application that outputs data in Json format (via Gson). I write that data to a file. That file is then read by a C++ application. The C++ application is using jsoncpp to deserialize the json. However, it appears that the C++ application cannot properly deserialize the Json (which is the whole point of using Json).
The problem seems to relate to the class name being included in the Gson output. Gson output sample:
{"nameOfClass":{"fieldName":"fieldvalue","secondFieldName":1}
As far as I can tell, "nameOfClass" is throwing off jsoncpp. Perhaps my jsoncpp deserialize method is incorrect? I have specific code to handle the different fields, but nothing that specifically handles that initial class name. Is that something I need to handle?
Short answer: user error
Longer answer:
It turns out I was serializing the wrong object. The class of this object CONTAINS a field of type "nameOfClass". What I wanted was that FIELD to be serialized, not the whole object. Because of my inexperience with Json and unfortunate choice of the field's name, I thought the output was malformed. Once I got the field from the object and serialized that, everything was fine.

How does IPCRenderer.send() serialize data to JSON?

I am trying to send information about an error event using ipcRenderer.send("error", errorObject) but my Error object gets serialized to '{}' in the listener. Now, I know that ipcRenderer serializes objects to JSON internally (More information here: https://electronjs.org/docs/api/ipc-renderer) so I want to find out what method is called for serialization to JSON internally so that I can try to override it in my code. Can anyone help?
I guess it's using JSON.stringify() but it's probably serialized for security reason so maybe it's better to not override it. BTW I don't think override JSON.stringify() is a good practice in any way. I didn't notice ipcRenderer.send serialized data, I pass plain JavaScript Object as data and don't parse it on ipcMain side.

Efficient way of parsing Jax-ws Restful Response

I need to parse the jax-ws rest response and I tried the following two ways of parsing the response.Both works good.But I am in need to know the best efficient way of implementation.Please provide me your view.
First Approach:
Use getEntity Object and get the response as Input Stream.
Using Jackson ObjectMapper readValue() -covert the inputstream to java
object.
Using getters and setters of nested java class get the response objects member values.
Second Approach:
Use getEntity Object and get the response as Input Stream and and
convert the Input Stream to String.
Using Google Json API,convert the string to json object.
Using Json parser and get the nested objects member values.
I would say the first approach is better for two reasons:
You don't go through the intermediate process of reading the response payload into String
The setter methods that are called during Jackson deserialization may perform validation on input and throw appropriate exceptions, so you do validation during deserialization.
Maybe not a general answer to this question but another variant of what you're describing under "First approach". I would start with a generic data structure and would only introduce an extra bean if necessary. I wouldn't use String to pass structured data around.
Use jackson to convert the JSON response to a
Map<String,Object> or JsonNode.
Advantage:
You don't need to implement a specialized bean class. Even a very simple bean can become unhandy over time (if format changes or new nested structures are added to the json response, etc.). It also introduces some kind of metaphor to your code which sometimes helps but also can be misleading.
Map<String,Object> is in the JDK and offers a good interface to access data. You don't have to change any interfaces even if the JSON format changes.
You can always pass your data in form of a Map<String,Object>
Disadvantage
Data Encapsulation. The map is a very close representation of the input data and therefore offers not same level of abstraction like a bean.

JSon Stringify and Parse implementation

JSON Parse and stringify are the two function to convert a string into object (by deserialization) and an object into string (by serialization of the object).
Can anyone explain me the exact methodology for the both function? I expect an answer describing the steps in detail.
Note: I am not looking for a difference but the methodology for implementation.
Just the steps in simple English words.

How to filter special characters in json?

I use java generated json like this:
[{"nickname":"abc,def"},{"nickname":"abc'def"},{"nickname":"abc"def"}]
when I execute eval(), it raises exception.
I wanna keep comma and quotes in the json.
how to handle this kind of situation?
You have an exception because your json in invalid! How did you generate it? In whatever language you use, build your data structure and call a json encode function (e.g. json_encode in PHP) to generate valid json.
In this specific example, you need to escape the double quote in the last string:
[{"nickname":"abc,def"},{"nickname":"abc'def"},{"nickname":"abc\"def"}]
Note: using eval is not safe, you need to use JSON.parse()!
You need to handle this situation by parsing JSON.
eval() does not parse JSON.
You need to call JSON.parse(), which does parse JSON and will work fine.
The other part of the solution is to generate valid JSON.
"abc"def" is not valid JSON.
You need to change your Java code to use an actual JSON library (such as gson or jackson) which will generate valid JSON.