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