Simple Java Jackson Serialize Objects to JSON and convert them back To Object

In this example we will try to serialize Java Objects to JSON , and read converting them back to Java Objects.

For this we need two libraries in our maven project:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>

This is a really simple example, and i'll start with converting the objects and at the end i'll add the code for creating domain objects.

We need an  ObjectMapper :

        final ObjectMapper objectMapper = new ObjectMapper();

then we will create the Object (in this case will be an order) and convert it to JSON using objectMapper.writeValueAsString:

        Order order = createOrder();
        String orderAsString =  objectMapper.writeValueAsString(order);

the output (I used objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(order) ):

{
  "id" : 1,
  "customer" : {
    "id" : 1,
    "name" : "First Customer"
  },
  "orderedProducts" : [ {
    "id" : 1,
    "product" : {
      "id" : 1,
      "name" : "Product One",
      "price" : 1.5
    },
    "quantity" : 1
  }, {
    "id" : 2,
    "product" : {
      "id" : 1,
      "name" : "Product Two",
      "price" : 5.0
    },
    "quantity" : 5
  } ]
}

now we have a JSON string , and we will try to convert it back to Java Object using objectMapper.readValue:

            Order orderFromJson = objectMapper.readValue(orderAsString, Order.class);
            System.out.println(orderFromJson);
Order{id=1, customer=org.newtutorials.jackson.standard.model.Customer@56f4468b, orderedProducts=[org.newtutorials.jackson.standard.model.OrderedProduct@6cc4c815, org.newtutorials.jackson.standard.model.OrderedProduct@3a82f6ef]}

and the code for creating the order :

private static Order createOrder() {
        Customer customer = new CustomerBuilder()
                .setId(1l)
                .setName("First Customer")
                .createCustomer();
        Product product = new ProductBuilder()
                .setId(1l)
                .setName("Product One")
                .setPrice(1.5d)
                .createProduct();
        Product productTwo = new ProductBuilder()
                .setId(1l)
                .setName("Product Two")
                .setPrice(5d)
                .createProduct();
        List<OrderedProduct> orderedProducts = new ArrayList<>();
        orderedProducts.add(
                new OrderedProductBuilder()
                        .setId(1l)
                        .setProduct(product)
                        .setQuantity(1)
                        .createOrderedProduct());
        orderedProducts.add(
                new OrderedProductBuilder()
                        .setId(2l)
                        .setProduct(productTwo)
                        .setQuantity(5)
                        .createOrderedProduct());
        return new OrderBuilder()
                .setId(1l)
                .setCustomer(customer)
                .setOrderedProducts(orderedProducts)
                .createOrder();
    }

 

you can download the source code from github : see GitHub Jackson simple example 

Category: Java Tutorials