How to fluently build JSON in Java?

后端 未结 9 1001
别跟我提以往
别跟我提以往 2020-12-07 13:43

I\'m thinking of something like:

String json = new JsonBuilder()
  .add(\"key1\", \"value1\")
  .add(\"key2\", \"value2\")
  .add(\"key3\", new JsonBuilder()         


        
相关标签:
9条回答
  • 2020-12-07 14:23

    The reference implementation includes a fluent interface. Check out JSONWriter and its toString-implementing subclass JSONStringer

    0 讨论(0)
  • 2020-12-07 14:29

    it's much easier than you think to write your own, just use an interface for JsonElementInterface with a method string toJson(), and an abstract class AbstractJsonElement implementing that interface,

    then all you have to do is have a class for JSONProperty that implements the interface, and JSONValue(any token), JSONArray ([...]), and JSONObject ({...}) that extend the abstract class

    JSONObject has a list of JSONProperty's
    JSONArray has a list of AbstractJsonElement's

    your add function in each should take a vararg list of that type, and return this

    now if you don't like something you can just tweak it

    the benifit of the inteface and the abstract class is that JSONArray can't accept properties, but JSONProperty can accept objects or arrays

    0 讨论(0)
  • 2020-12-07 14:32

    You can use one of Java template engines. I love this method because you are separating your logic from the view.

    Java 8+:

    <dependency>
      <groupId>com.github.spullara.mustache.java</groupId>
      <artifactId>compiler</artifactId>
      <version>0.9.6</version>
    </dependency>
    

    Java 6/7:

    <dependency>
      <groupId>com.github.spullara.mustache.java</groupId>
      <artifactId>compiler</artifactId>
      <version>0.8.18</version>
    </dependency>
    

    Example template file:

    {{#items}}
    Name: {{name}}
    Price: {{price}}
      {{#features}}
      Feature: {{description}}
      {{/features}}
    {{/items}}
    

    Might be powered by some backing code:

    public class Context {
      List<Item> items() {
        return Arrays.asList(
          new Item("Item 1", "$19.99", Arrays.asList(new Feature("New!"), new Feature("Awesome!"))),
          new Item("Item 2", "$29.99", Arrays.asList(new Feature("Old."), new Feature("Ugly.")))
        );
      }
    
      static class Item {
        Item(String name, String price, List<Feature> features) {
          this.name = name;
          this.price = price;
          this.features = features;
        }
        String name, price;
        List<Feature> features;
      }
    
      static class Feature {
        Feature(String description) {
           this.description = description;
        }
        String description;
      }
    }
    

    And would result in:

    Name: Item 1
    Price: $19.99
      Feature: New!
      Feature: Awesome!
    Name: Item 2
    Price: $29.99
      Feature: Old.
      Feature: Ugly.
    
    • mustache: https://github.com/spullara/mustache.java
    • But there is also Jinja: https://github.com/HubSpot/jinjava
    • And carrot: https://github.com/codeka/carrot
    0 讨论(0)
提交回复
热议问题