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