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