groovy.json.JsonBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-json Show documentation
Show all versions of groovy-json Show documentation
Groovy: A powerful, dynamic language for the JVM
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package groovy.json;
import groovy.lang.Closure;
import groovy.lang.GroovyObjectSupport;
import groovy.lang.Writable;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* A builder for creating JSON payloads.
*
* This builder supports the usual builder syntax made of nested method calls and closures,
* but also some specific aspects of JSON data structures, such as list of values, etc.
* Please make sure to have a look at the various methods provided by this builder
* to be able to learn about the various possibilities of usage.
*
* Example:
*
* def builder = new groovy.json.JsonBuilder()
* def root = builder.people {
* person {
* firstName 'Guillame'
* lastName 'Laforge'
* // Named arguments are valid values for objects too
* address(
* city: 'Paris',
* country: 'France',
* zip: 12345,
* )
* married true
* // a list of values
* conferences 'JavaOne', 'Gr8conf'
* }
* }
*
* // creates a data structure made of maps (Json object) and lists (Json array)
* assert root instanceof Map
*
* assert builder.toString() == '{"people":{"person":{"firstName":"Guillame","lastName":"Laforge","address":{"city":"Paris","country":"France","zip":12345},"married":true,"conferences":["JavaOne","Gr8conf"]}}}'
*
*
* @author Guillaume Laforge
* @author Andrey Bloshetsov
* @since 1.8.0
*/
public class JsonBuilder extends GroovyObjectSupport implements Writable {
private final JsonGenerator generator;
private Object content;
/**
* Instantiates a JSON builder.
*/
public JsonBuilder() {
this.generator = JsonOutput.DEFAULT_GENERATOR;
}
/**
* Instantiates a JSON builder with a configured generator.
*
* @param generator used to generate the output
* @since 2.5.0
*/
public JsonBuilder(JsonGenerator generator) {
this.generator = generator;
}
/**
* Instantiates a JSON builder with some existing data structure.
*
* @param content a pre-existing data structure
*/
public JsonBuilder(Object content) {
this.content = content;
this.generator = JsonOutput.DEFAULT_GENERATOR;
}
/**
* Instantiates a JSON builder with some existing data structure
* and a configured generator.
*
* @param content a pre-existing data structure
* @param generator used to generate the output
* @since 2.5.0
*/
public JsonBuilder(Object content, JsonGenerator generator) {
this.content = content;
this.generator = generator;
}
public Object getContent() {
return content;
}
/**
* Named arguments can be passed to the JSON builder instance to create a root JSON object
*
* Example:
*
* def json = new groovy.json.JsonBuilder()
* json name: "Guillaume", age: 33
*
* assert json.toString() == '{"name":"Guillaume","age":33}'
*
*
* @param m a map of key / value pairs
* @return a map of key / value pairs
*/
public Object call(Map m) {
content = m;
return content;
}
/**
* A list of elements as arguments to the JSON builder creates a root JSON array
*
* Example:
*
* def json = new groovy.json.JsonBuilder()
* def result = json([1, 2, 3])
*
* assert result instanceof List
* assert json.toString() == "[1,2,3]"
*
*
* @param l a list of values
* @return a list of values
*/
public Object call(List l) {
content = l;
return content;
}
/**
* Varargs elements as arguments to the JSON builder create a root JSON array
*
* Example:
*
* def json = new groovy.json.JsonBuilder()
* def result = json 1, 2, 3
*
* assert result instanceof List
* assert json.toString() == "[1,2,3]"
*
*
* @param args an array of values
* @return a list of values
*/
public Object call(Object... args) {
List