Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robust deserialization of maps #262

Closed
apinaut opened this issue Jul 15, 2013 · 2 comments
Closed

Robust deserialization of maps #262

apinaut opened this issue Jul 15, 2013 · 2 comments

Comments

@apinaut
Copy link

apinaut commented Jul 15, 2013

Hi,

one of our customers is using PHP to connect to our backend. Every
time he sends empty values for a map with PHP, the JSON content is
parsed as array (since in PHP, there are only associative arrays
whereas emtpy ones look identical to usual arrays).

If one could modify the MapDeserializer class, method  "deserialize"
to the following:

[...]
         if ( t != JsonToken.START_OBJECT && t !=
JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT )
         {
             // [JACKSON-620] (empty) String may be ok however:
             if ( t == JsonToken.VALUE_STRING )
             {
                 return ( Map<Object, Object> )
this._valueInstantiator.__createFromString( jp.getText( ) );

             }
             // empty PHP map looks like an array
             if ( t == JsonToken.START_ARRAY )
             {
                 return new HashMap<Object, Object>( );
             }

             throw ctxt.mappingException( getMapClass( ) );
         }
[...]

This should solve the problem. Maybe it would be nice to make this
configurable as a feature in the mapper.
@cowtowncoder
Copy link
Member

Have you tried running that code? It does not match END_ARRAY; nor consider what happens to contents -- even though in this particular case you expect empty array, it has to consider case of elements within array.

I think something more is needed to also allow this behaviour, because it can easily mask actual error cases (content mismatch).

So I agree that something similar to above would help handle PHP oddity of empty array to represent empty Map (and/or Objects). But there is bit more design to do to make sure the new feature works with other aspects of data binding.

@cowtowncoder
Copy link
Member

Now there is #540, which allows deserializing ' [ ]' as null for POJOs as well as Maps.
Since that might cover this use case, I will close the issue.

If there is still need to instead allow construction of an empty Map, please re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants