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