001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.examples.core.json;
018
019import java.util.*;
020
021import org.apache.juneau.examples.core.pojo.*;
022import org.apache.juneau.json.*;
023
024/**
025 * Sample class which shows the complex usage of JsonSerializer and JsonParser.
026 *
027 */
028public class JsonComplexExample {
029
030   /**
031    * Serializing PojoComplex bean into Json type
032    * and Deserialize back to PojoComplex instance type.
033    *
034    * @param args Unused.
035    * @throws Exception Unused.
036    */
037   public static void main(String[] args) throws Exception {
038      // Juneau provides static constants with the most commonly used configurations
039      // Get a reference to a serializer - converting POJO to flat format
040      /**
041       * Produces
042       * {"innerPojo":{"name":"name0","id":"1.0"},
043       * "values":{"setOne":[{"name":"name1","id":"1.1"},{"name":"name2","id":"1.1"}],
044       * "setTwo":[{"name":"name1","id":"1.2"},{"name":"name2","id":"1.2"}]},"id":"pojo"}
045       */
046      var jsonSerializer = JsonSerializer.DEFAULT;
047      // Get a reference to a parser - converts that flat format back into the POJO
048      var jsonParser = JsonParser.DEFAULT;
049
050      // Fill some data to a PojoComplex bean
051      var values = new HashMap<String,List<Pojo>>();
052      var setOne = new ArrayList<Pojo>();
053      setOne.add(new Pojo("1.1", "name1"));
054      setOne.add(new Pojo("1.1", "name2"));
055      var setTwo = new ArrayList<Pojo>();
056      setTwo.add(new Pojo("1.2", "name1"));
057      setTwo.add(new Pojo("1.2", "name2"));
058      values.put("setOne", setOne);
059      values.put("setTwo", setTwo);
060      var pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
061
062      var flat = jsonSerializer.serialize(pojoc);
063
064      // Print out the created POJO in JSON format.
065      System.out.println(flat);
066
067      var parse = jsonParser.parse(flat, PojoComplex.class);
068
069      assert parse.getId().equals(pojoc.getId());
070      assert parse.getInnerPojo().getName().equals(pojoc.getInnerPojo().getName());
071      assert parse.getInnerPojo().getId().equals(pojoc.getInnerPojo().getId());
072
073      // The object above can be parsed thanks to the @Beanc(properties = id,name) annotation on Pojo
074      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
075   }
076}