Jackson Full Data Binding: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Jackson =Overview= =JSON to Java= ==JSON to Java Code Example== {{External|}} =Java to JSON= ==Java to JSON Code Example==")
 
No edit summary
Line 6: Line 6:


=JSON to Java=
=JSON to Java=
<pre>
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper om = new ObjectMapper();
Token token = on.readValue(inputStream, Token.class);
</pre>
This is a generic conversion method, assuming that the JSON content matches the internal structure of the type <T>:
<pre>
public static <T> T fromJson(String json, Class<? extends T> c) throws JsonConversionException {
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(new ByteArrayInputStream(json.getBytes()), c);
    }
    catch (Exception e) {
      throw new JsonConversionException(e);
    }
}
</pre>


==JSON to Java Code Example==
==JSON to Java Code Example==

Revision as of 19:14, 26 February 2017

Internal

Overview

JSON to Java

import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper om = new ObjectMapper();
Token token = on.readValue(inputStream, Token.class);

This is a generic conversion method, assuming that the JSON content matches the internal structure of the type <T>:

public static <T> T fromJson(String json, Class<? extends T> c) throws JsonConversionException {

    try {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(new ByteArrayInputStream(json.getBytes()), c);
    }
    catch (Exception e) {
       throw new JsonConversionException(e);
    }
}


JSON to Java Code Example


Java to JSON

Java to JSON Code Example