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

Support BuiltMap<String, Object> like JSON #134

Closed
davidmorgan opened this issue Mar 1, 2017 · 6 comments
Closed

Support BuiltMap<String, Object> like JSON #134

davidmorgan opened this issue Mar 1, 2017 · 6 comments

Comments

@davidmorgan
Copy link
Collaborator

JSON protocols can have a Map<String, Object> where Object is any JSON primitive. Support this somehow with StandardJsonPlugin.

@zoechi
Copy link
Contributor

zoechi commented Mar 6, 2017

Also List<Object>

Do you have short-term plans to provide something like the mentioned JSON primitive?
For a current project this would be required so that built_value could be used.

@davidmorgan
Copy link
Collaborator Author

I will try to look at it soon -- but can't promise anything. If you like you can try it yourself. New serializable types and serializers can be added as you like, e.g.:

class JsonObject {
  final Object object;

  JsonObject(this.object);

  ... utility methods here, e.g. "asInt", "asDouble", "asList"...
  ... also hashCode, operator== if you want those to work...
}

class JsonObjectSerializer implements PrimitiveSerializer<JsonObject> {
  @override
  final Iterable<Type> types = new BuiltList<Type>([JsonObject]);
  @override
  final String wireName = 'JsonObject';

  @override
  Object serialize(Serializers serializers, JsonObject object,
      {FullType specifiedType: FullType.unspecified}) {
    return object;
  }

  @override
  int deserialize(Serializers serializers, Object serialized,
      {FullType specifiedType: FullType.unspecified}) {
    return new JsonObject(serialized);
  }
}

// Add JsonObjectSerializer to Serializers to use it.
Serializers mySerializers = (serializers.toBuilder()
    ..add(new JsonObjectSerializer())).build();
mySerializers.serialize(...)

@zoechi
Copy link
Contributor

zoechi commented Mar 6, 2017

Thanks for the info!
Of course no promise - it's the open source world :)
I'll have a look, but first I need to better understand the whole builder stuff - never used anything like that.

@zoechi
Copy link
Contributor

zoechi commented Mar 11, 2017

JsonObject get keyValues;
works fine so far.

I also tried
MapJsonObject get keyValues; and ListJsonObject get keyValues;, but that fails when deserializing.
Is this supposed to work?

@davidmorgan
Copy link
Collaborator Author

It should work better to use

BuiltMap<String, JsonObject>

instead of MapJsonObject, and

BuiltList

instead of ListJsonObject -- so I don't think these should be needed. Possibly I missed something?

@zoechi
Copy link
Contributor

zoechi commented Mar 13, 2017

Didn't think of that. Thanks for the pointer 👍

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

No branches or pull requests

2 participants