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