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.Json5Serializer;
026
027import java.util.Map;
028
029/**
030 * Sample class which shows the simple usage of JsonSerializer and JsonParser.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 * </ul>
034 */
035public class JsonSimpleExample {
036
037   /**
038    * Serializing Pojo bean into Json format and Deserialize back to Pojo instance type.
039    *
040    * @param args Unused.
041    * @throws Exception Unused.
042    */
043   @SuppressWarnings({ "unused", "rawtypes" })
044   public static void main(String[] args) throws Exception{
045      // Juneau provides static constants with the most commonly used configurations
046      // Get a reference to a serializer - converting POJO to flat format
047      // Produces
048      // {"name":"name","id":"id"}
049      JsonSerializer jsonSerializer = JsonSerializer.DEFAULT;
050      // Get a reference to a parser - converts that flat format back into the POJO
051      JsonParser jsonParser = JsonParser.DEFAULT;
052
053      Pojo pojo = new Pojo("id","name");
054
055      String flat = jsonSerializer.serialize(pojo);
056      // Print out the created POJO in JSON format.
057      System.out.println(flat);
058
059      Pojo parse = jsonParser.parse(flat, Pojo.class);
060
061      assert parse.getId().equals(pojo.getId());
062      assert parse.getName().equals(pojo.getName());
063
064      // Produces
065      // {name:'name',id:'id'}
066      String json5 = Json5Serializer.DEFAULT.serialize(pojo);
067      System.out.println(json5);
068
069      // Parse a JSON object (creates a generic JsonMap).
070      String json = "{name:'John Smith',age:21}";
071      Map m1 = jsonParser.parse(json, Map.class);
072
073      // Parse a JSON string.
074      json = "'foobar'";
075      String s2 = jsonParser.parse(json, String.class);
076
077      // Parse a JSON number as a Long or Float.
078      json = "123";
079      Long l3 = jsonParser.parse(json, Long.class);
080      Float f3 = jsonParser.parse(json, Float.class);
081
082      // The object above can be parsed thanks to the @Beanc(properties = id,name) annotation on Pojo
083      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
084   }
085}