Skip to content

free functions for explicit objects and arrays in initializer lists #8

Closed

Description

@nlohmann

Currently,

{{"a","b"},{"c","d"}}

will be treated as object, and there is no way to express it as array of two arrays with two strings each.

As tgockel points out:

I can't say it's a great solution, but in JSON Voorhees, I went with free functions called array and object to disambiguate the initializer_list syntax. It also solves the issue of {} ambiguously meaning an empty object or empty array (and accidentally making an array when given string_views or something).

Such free functions could solve the above problem:

{array({"a","b"}), array({"c","d"})}

Activity

  1. added a commit that references this issue on Jan 24, 2015
  2. self-assigned this
    on Jan 24, 2015
  3. nlohmann commented on Jan 24, 2015

    @nlohmann
    OwnerAuthor

    The array

    [["a","b"], ["c","d"]]

    can now be created with

    {json::array({"a", "b"}), json::array({"c", "d"})}

    Thereby json::array and json::object are functions to force a type in an initializer list.

  4. modified the milestone: on Jul 18, 2015
  5. added a commit that references this issue on May 7, 2021
  6. added a commit that references this issue on Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions