Skip to content

Decoding empty php associative arrays  #281

@patternoia

Description

@patternoia

Describe the proposed feature

In php the objects are often represented by associative arrays. Serializing them produces valid json object.

>>> $object = ["key" => "value"]
=> [
     "key" => "value",
   ]
>>> json_encode($object)
=> "{"key":"value"}"

However if this object becomes empty the associative array becomes normal empty array and serializing it produces an empty json array.

>>> unset($object["key"])
>>> json_encode($object)
=> "[]"

If this happens then my map<string, string> can not be deserialized from "[]" to an empty map with an exception.

Can this be handled so that empty map or an empty class instance will be created from "[]"?

Thanks.

What other libraries (C++ or other) have this feature?
Unaware, since I stick to jsoncons.

Include a code fragment with sample data that illustrates the use of this feature

jsoncons::ojson::parse("[]").as<std::vector<int>>()
(std::enable_if<true, std::vector<int, std::allocator<int> > >::type) {}
jsoncons::ojson::parse("[]").as<std::map<std::string,std::string>>()
jsoncons::json_runtime_error<std::domain_error, void> caught: Not an object

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions