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