Skip to content

Add jsonObjectOf and jsonArrayOf helper functions #2129

Open
@rhdunn

Description

@rhdunn

What is your use-case and why do you need this feature?

Maps provide emptyMap() and mapOf() helper functions for constructing maps. Lists provide emptyList() and listOf() helper functrions for constructing lists.

The element builders buildJsonObject and buildJsonArray are useful for more complex object/array construction, but the Of helpers are better and more readable for simple use cases such as constructing a Language Server Protocol Position object.

Example: Construct an LSP Position object:

val position = jsonObjectOf(
    "line" to JsonPrimitive(1),
    "character" to JsonPrimitive(8)
)

Example: Construct an LSP PositionEncodingKind array:

val positionEncodings = jsonArrayOf(JsonPrimitive("utf-32"), JsonPrimitive("utf-16"))

Describe the solution you'd like

It would be helpful if there were equivalents for JSON objects and arrays:

  1. emptyJsonObject -- Construct an empty JSON object.
  2. jsonObjectOf -- Construct a JSON object from a variadic argument array of (key, JSON element value) pairs.
  3. emptyJsonArray -- Constuct an empty JSON array.
  4. jsonArrayOf -- Construct a JSON array from a variadic argument array of JSON element values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QoLQuality of lifefeaturejson-buildersSuggested enhancements for JsonElements builders

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions