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 * <h5 class='section'>See Also:</h5><ul> 029 * </ul> 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 Pojo 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 String withWhitespace = JsonSerializer.create().ws().build().serialize(aPojo); 051 // the output will be padded with spaces after format characters 052 System.out.println(withWhitespace); 053 054 /** 055 * Produces 056 * {"name":"<\/pojo>","id":"a"} 057 */ 058 String escaped = JsonSerializer.create().escapeSolidus().build().serialize(aPojo); 059 // the output will have escaped / 060 System.out.println(escaped); 061 062 /** 063 * Produces 064 * { 065 * name: '</pojo>', 066 * id: 'a' 067 * } 068 */ 069 String configurableJson =JsonSerializer 070 .create() // Create a JsonSerializer.Builder 071 .simpleAttrs() // Simple mode 072 .ws() // Use whitespace 073 .sq() // Use single quotes 074 .build() 075 .serialize(aPojo); // Create a JsonSerializer 076 077 System.out.println(configurableJson); 078 079 080 } 081}