-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
TokenBuffer needs to store a token when writeObjectId is called #1068
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
Conversation
What specific problem is fixed here? A unit test would show intended behavior improvement. But further, I don't think |
Put another way: I don't doubt there could be a problem to fix, but without a way to verify it, I can't quite see how exactly this should work. Current code tries to keep track of it with |
I am bridging Jackson and the Mongo java driver 3.0 Codec framework. For this, I am implementing a JsonParser by wrapping a Mongo Bson reader. Things work quite well so far, but when I use the polymorphic facility of Jackson, the TokenBuffer comes into action and has it is not storing any token for ObjectId, I can't have polymorphism and ObjectId support. |
Right but what I do not quite understand is as to why the existing facility that |
It looks to me that the TokenBuffer should behave on ObjectId as it behaves for embedded objects. It misses a token indeed to disambiguate the two. My assumption is that when i am adding to the generator a FIELD and then and ObjectId, I am expecting to be able to parse a FIELD token and then to get an other token (EMBEDDED ?) to tell that what comes next is an ObjectId. |
The following sequence works. TokenBuffer buf = new TokenBuffer(null, true);
Object id = new Object();
buf.writeObjectId(id);
//field needs to come after to post objectid into segment by side effect
buf.writeFieldName("_id");
JsonParser p = buf.asParser();
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("_id", p.getCurrentName());
assertEquals(id, p.getObjectId()); It looks like the contract of the JsonParser is to attach an ObjectId to a field but not to consider it as an independant token (like an embedded object) . It looks odd. At least in my case i don't manage writing the FIELD to the JsonGenerator. This is done by Jackson and i get control back only through my custom JsonSerializer so that very sequence can not be reproduced. |
@gcxRun You may be misunderstanding concept of embedder object here: it is very different from that of Object Id, and two are not related in any way.
Object Id, however, is piece of metadata attached to values. Or, rather, is for some data formats: currently YAML has this concept; and I am guessing BSON possibly as well. For data formats that do not have native Object Ids, there is actual data entry (like String value) that is used instead. For Token Buffer, then, support is needed for as-metadata-object-ids, which are meant to be attached to other tokens. They are not represented as value tokens themselves, and use of My problem here is that I only see your work-around, but not the problem you are trying to solve. It would be good to understand more about the original problem you have with BSON. |
My goal is to bridge Jackson and the Mongo driver v3. This is close to the mapping approach offered by Jongo or MongoJack. I understand more now, what you mean by ObjectId. Looking at the bson spec, ObjectId is just a specific type, that does not exist in json, pretty much like date. This is not an attribute attached to an other value. So let's forget about the ObjectId mismatch. Embedded object is probably a more natural fit. But why does the TokenBuffer relies on the _objectCodec. As the TokenBuffer caches token from another parser, the delegation to the custom codec has already been used, hasn't it ? |
@gcxRun Ah. Thank you for explanation -- I can see why you were thinking that approach, since for your case such ids are embedded opaque objects, and not like what jackson core models as "object ids". They just have same name, but different semantics. So we are on agreement here. As to As So... I guess it would be good to try another approach here too. |
I gave a try to adding a writeEmbeddedObject method to JsonGenerator (along with its canWriteEmbeddedObject companion). TokenBuffer then can explicitely push embedded object value, its parser then emits embedded objet tokens. I don't have to pop/push the ObjectCodec from my custom serializer. |
@gcxRun Another alternative would be that if you are sure you are dealing with |
I can't be sure if i am dealing with a TokenBuffer, I've seen it used only when polymorphism kicks in and jackson starts parsing fields before knowing the class to construct. I could check and then cast of course, but this is close to the saving and restoring of the ObjectCodec I am doing right now : the workaround works as long the implementation does not change. |
@gcxRun Sounds good, let's do that then. I will file a |
No description provided.