public class JsonTemplate extends Object
{
"city" : "Utrecht",
"street" : "Musicallaan",
"number" : 413
}
You can use the following code snippet to create the expected json.
String template = "{city:Utrecht, street:Musicallaan, number:413}"
String json = new JsonTemplate(template).prettyString();
String template = "{city:@s, street:@s, number:@i}"
@s, @i refer to value producers.| 构造器和说明 |
|---|
JsonTemplate(String template) |
| 限定符和类型 | 方法和说明 |
|---|---|
String |
compactString()
Produces a compact json string.
|
String |
getTemplate()
Returns the input json template
|
String |
prettyString()
Produces a json string with identations.
|
JsonTemplate |
withDefaultTypeName(String typeName)
Registers the default type name.
|
JsonTemplate |
withValueProducer(IValueProducer valueProducer)
Registers a node producer.
|
JsonTemplate |
withVar(String variableName,
Object variable)
Registers a variable which is used in the template.
|
JsonTemplate |
withVars(Map<String,Object> variables)
Registers a map of variables which are used in the template.
|
public JsonTemplate(String template)
public JsonTemplate withVar(String variableName, Object variable)
String json = new JsonTemplate("{city:$cityVar}")
.withVar("cityVar", "Utrecht").compactString();
In the end, the value of json is
{"city":"Utrecht"}
The variable can be set as a single parameter. For example,
{city:@s($var)}
If var refers to a collection or an array, e.g., {"Amsterdam", "Utrecht"}.
The template is equal to the list parameter form {city:@s(Amsterdam, Utrecht)}
which selects one value from the list parameter.
If var refers to a map, e.g., mapVar.put("length", 10). The template is equal to the map parameter form {city:@s(length=10)}.
Otherwise, the string representation of the variable is used.
The variable can be set as an element of the list parameter. For example,
{city:@s(Amsterdam, $anotherCity)}. No matter what the type
of $anotherCity is, the string representation of the variable is always
used.
The variable can be set as a value in the map parameter. For example,
{city:@s(length=$expectedLength)}. No matter what the type
of $expectedLength is, the string representation of the variable is always
used.
The variable can be set as a json value. For example,
{city:$cityVar}
If $cityVar is null, it is converted to a json null value.
If $cityVar is an integer or a float, it is converted to a json numeric value.
If $cityVar is a boolean, it is converted to a json boolean value.
If $cityVar is an array or a collection, it is converted to a json array.
If $cityVar is a map, it is converted to a json object.
Otherwise, it is converted to a json string object.
variableName - name of the variable without the leading '$'variable - variable valuepublic JsonTemplate withVars(Map<String,Object> variables)
variables - map of variableswithVar(String, Object)public JsonTemplate withValueProducer(IValueProducer valueProducer)
The pre-installed node producers are:
valueProducer - the customized value producerpublic JsonTemplate withDefaultTypeName(String typeName)
SmartValueProducer.
Example:
{ obj1: @i{fieldA}, obj2: {fieldB} }
the default type of root is not specified, by default it is @smart
typeName - the default type namepublic String getTemplate()
public String compactString()
public String prettyString()
compactString()Copyright © 2019. All rights reserved.