serialize enum fields with gson

By default, Gson just serialize the ‘name’ of the Enum which might not be enough since we might need also want to carry all the fields during the serialization. To achieve this we need to has our own gson adaptor and make use of reflection. usage: Enum class: Test Code: Result:

ignore field when serialize/deserialize with Gson

While we use Gson for serializing/deserializing, some field we might want to ignore. A direct way is to make the field transient. Alternatively we could use the @Expose to annotate the field we want to be handled. However there is currently no way to ignore in one way. Here is a solution

spring config custom httpMessageConverter

in spring 4.1.2, the GsonHttpMessageConverter is added so that we could use it to replace the jackson converter for json serialization and deserialization. To added it, we could config our own RequestMappingHandlerAdapter and add the gson one to the “messageConverters” field. Also we would have to add bean validator for the RequestMappingHandlerAdapter. By doing this, i got several validation…

convert string to json object

In JS To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in…