Class JsonBuilder


  • public class JsonBuilder
    extends java.lang.Object
    A helper to build a string in JSON format.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String build()  
      static JsonBuilder json()
      Creates new instance of this builder.
      JsonBuilder withCode​(java.lang.String code)
      Specifies field named "code" for the JSON output.
      JsonBuilder withField​(java.lang.String field, boolean value)
      Specifies field value for the JSON output.
      JsonBuilder withField​(java.lang.String field, JsonBuilder builder)
      Specifies field value for the JSON output.
      JsonBuilder withField​(java.lang.String field, java.lang.Character value)
      Specifies a character field value for the JSON output.
      JsonBuilder withField​(java.lang.String field, java.lang.Number number)
      Specifies field value for the JSON output.
      JsonBuilder withField​(java.lang.String field, java.lang.String value)
      Specifies field value for the JSON output.
      JsonBuilder withField​(java.lang.String field, java.util.Collection<?> values)
      Specifies field value for the JSON output.
      JsonBuilder withField​(java.lang.String field, java.util.Date date)
      Specifies date field value for the JSON output.
      JsonBuilder withFieldValues​(java.lang.String field, java.lang.Object... values)
      Specifies field value for the JSON output.
      JsonBuilder withId​(java.lang.String id)
      Specifies field named "id" for the JSON output.
      JsonBuilder withLocalizedAttributes​(java.util.Map<java.lang.String,​java.lang.String>... attributes)
      Specifies the localized attributes for the JSON output.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • json

        public static JsonBuilder json()
        Creates new instance of this builder.
        Returns:
        new builder instance, which has no fields specified and results in "{}" JSON when built.
      • withCode

        public JsonBuilder withCode​(java.lang.String code)
        Specifies field named "code" for the JSON output. This is the same as calling withField("code", code)
        Parameters:
        code - value for the "code" field
        Returns:
        a builder with the field specified
      • withId

        public JsonBuilder withId​(java.lang.String id)
        Specifies field named "id" for the JSON output. This is the same as calling withField("id", code)
        Parameters:
        id - value for the "id" field
        Returns:
        a builder with the field specified
      • withField

        public JsonBuilder withField​(java.lang.String field,
                                     JsonBuilder builder)
        Specifies field value for the JSON output.
        Parameters:
        field - name of the JSON field, which prints before the ":" separator. For example, address in {"address": {...}}
        builder - nested JSON object to be used as value for the field
        Returns:
        a builder with the field specified.
      • withField

        public JsonBuilder withField​(java.lang.String field,
                                     java.lang.Number number)
        Specifies field value for the JSON output.
        Parameters:
        field - name of the JSON field, which prints before the ":" separator. For example, address in {"address": {...}}
        number - numeric value for the field
        Returns:
        a builder with the field specified.
      • withField

        public JsonBuilder withField​(java.lang.String field,
                                     boolean value)
        Specifies field value for the JSON output.
        Parameters:
        field - name of the JSON field, which prints before the ":" separator. For example, address in {"address": {...}}
        value - boolean value for the field
        Returns:
        a builder with the field specified.
      • withField

        public JsonBuilder withField​(java.lang.String field,
                                     java.util.Date date)
        Specifies date field value for the JSON output.
        Parameters:
        field - name of the JSON field, which prints before the ":" separator. For example, address in {"address": {...}}
        date - date value for the field
        Returns:
        a builder with the field specified.
      • withFieldValues

        public JsonBuilder withFieldValues​(java.lang.String field,
                                           java.lang.Object... values)
        Specifies field value for the JSON output.
        Parameters:
        field - name of the JSON field, which prints before the ":" separator. For example, reviews in {"reviews": [...]}
        values - a collection of values for the field
        Returns:
        a builder with the field specified.
      • withField

        public JsonBuilder withField​(java.lang.String field,
                                     java.util.Collection<?> values)
        Specifies field value for the JSON output.
        Parameters:
        field - name of the JSON field, which prints before the ":" separator. For example, reviews in {"reviews": [...]}
        values - nested JSON objects to be used as a collection value for the field
        Returns:
        a builder with the field specified.
      • withField

        public JsonBuilder withField​(java.lang.String field,
                                     java.lang.Character value)
        Specifies a character field value for the JSON output.
        Parameters:
        field - name of the JSON field, which prints before the ":" separator. For example, country in {"country": "USA"}
        value - value of the JSON field, which prints after the ":" separator. For example, 'a' in {"char": "a"}
        Returns:
        a builder with the field specified.
      • withField

        public JsonBuilder withField​(java.lang.String field,
                                     java.lang.String value)
        Specifies field value for the JSON output.
        Parameters:
        field - name of the JSON field, which prints before the ":" separator. For example, country in {"country": "USA"}
        value - value of the JSON field, which prints after the ":" separator. For example, USA in {"country": "USA"}
        Returns:
        a builder with the field specified.
      • withLocalizedAttributes

        @SafeVarargs
        public final JsonBuilder withLocalizedAttributes​(java.util.Map<java.lang.String,​java.lang.String>... attributes)
        Specifies the localized attributes for the JSON output.
        Parameters:
        attributes - An array of Maps with the name of the localized attribute as the key, and the localized value as the value
        Returns:
        a builder with the specified localized attributes.
      • build

        public java.lang.String build()