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.commons.logging.*;
023import org.apache.juneau.examples.core.pojo.*;
024import org.apache.juneau.marshall.json.*;
025import org.apache.juneau.marshall.json5.*;
026
027/**
028 * Json configuration example.
029 *
030 */
031public class JsonConfigurationExample {
032
033   /**
034    * Examples on Json Serializers configured using properties
035    * defined in JsonSerializer class
036    *
037    * @param args Unused.
038    * @throws Exception Unused.
039    */
040   public static void main(String[] args) throws Exception {
041      var aPojo = new Pojo("a", "</pojo>");
042      // Json Serializers can be configured using properties defined in JsonSerializer
043      /**
044       * Produces
045       * {
046       *    "name": "</pojo>",
047       *    "id": "a"
048       * }
049       */
050      var withWhitespace = JsonSerializer.create().ws().build().write(aPojo);
051      // the output will be padded with spaces after format characters
052      RichLogger.getLogger(JsonConfigurationExample.class).info(withWhitespace);
053
054      /**
055       * Produces
056       * {"name":"<\/pojo>","id":"a"}
057       */
058      var escaped = JsonSerializer.create().escapeSolidus().build().write(aPojo);
059      // the output will have escaped /
060      RichLogger.getLogger(JsonConfigurationExample.class).info(escaped);
061
062      /**
063       * Produces
064       * {
065       *    name: '</pojo>',
066       * id: 'a'
067       * }
068       */
069      var configurableJson = Json5Serializer
070         .create()  // Create a Json5Serializer.Builder
071         .ws()  // Use whitespace
072         .sq()  // Use single quotes
073         .build()
074         .write(aPojo);  // Create a Json5Serializer
075
076      RichLogger.getLogger(JsonConfigurationExample.class).info(configurableJson);
077
078   }
079}