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.commons.logging.*;
025import org.apache.juneau.examples.core.pojo.*;
026import org.apache.juneau.marshall.json.*;
027import org.apache.juneau.marshall.json5.*;
028
029/**
030 * Sample class which shows the simple usage of JsonSerializer and JsonParser.
031 *
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({
042      "java:S125", // S125: example output comments document expected output
043      "rawtypes", // Raw types necessary for generic type handling
044      "unused" // main(String[] args) - args required by JVM signature but unused in example
045   })
046   public static void main(String[] args) throws Exception {
047      // Juneau provides static constants with the most commonly used configurations
048      var jsonSerializer = JsonSerializer.DEFAULT;
049      // Get a reference to a parser - converts that flat format back into the POJO
050      var jsonParser = JsonParser.DEFAULT;
051
052      var pojo = new Pojo("id", "name");
053
054      var flat = jsonSerializer.write(pojo);
055      // Print out the created POJO in JSON format.
056      RichLogger.getLogger(JsonSimpleExample.class).info(flat);
057
058      var parse = jsonParser.read(flat, Pojo.class);
059
060      assert parse.getId().equals(pojo.getId());
061      assert parse.getName().equals(pojo.getName());
062
063      var json5 = Json5Serializer.DEFAULT.write(pojo);
064      RichLogger.getLogger(JsonSimpleExample.class).info(json5);
065
066      // Parse a JSON object (creates a generic JsonMap).
067      var json = "{name:'John Smith',age:21}";
068      Map m1 = jsonParser.read(json, Map.class);
069
070      // Parse a JSON string.
071      json = "'foobar'";
072      var s2 = jsonParser.read(json, String.class);
073
074      // Parse a JSON number as a Long or Float.
075      json = "123";
076      var l3 = jsonParser.read(json, Long.class);
077      var f3 = jsonParser.read(json, Float.class);
078
079      // The object above can be parsed thanks to the @BeanCtor(properties = id,name) annotation on Pojo
080      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
081   }
082}