Skip to content

The "builder" API #2958

@MukjepScarlet

Description

@MukjepScarlet

Description

(Idea by @eamonnmcmanus )

We want to build a JsonObject/Array in a simple way like:

Map<String, Object> user = Map.of(
             "id", 1001,
             "name", "Alice",
             "age", 25,
             "tags", List.of("Java", "Kotlin", "Scala"),
             "address", Map.of("city", "Shanghai", "street", "Xujiahui")
         );

JsonObject userJson = JsonObject.parse(user);

However, because of Java's type system, we can only do:

// JsonObject.class
static JsonObject parse(Map<String, ?>)
static JsonObject of(Map<String, ? extends JsonElement>)

// JsonArray.class
static JsonArray parse(Iterable<?>)
static JsonArray of(Iterable<? extends JsonElement>)

// JsonPrimitive
static JsonPrimitive parse(Object) // ???

// JsonElelement
static JsonElement parse(Object)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions