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