nlight-jdev / jcouchdb

Automatically exported from code.google.com/p/jcouchdb
Other
0 stars 0 forks source link

Feature request: create/update document with provided JSON serializar #58

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Database#getDocument can use provided JSON deserializer as a method argument. I 
think that Document#create/upudate should also accept JSON serializer as a 
method argument. Actually i need it ;-). The patch is attached.

Original issue reported on code.google.com by roman.pi...@gmail.com on 1 Sep 2010 at 8:42

Attachments:

GoogleCodeExporter commented 8 years ago
The theory behind the parser being per-method and the generator only per-db was 
that parsing a far more complex and depends far more on the context.

You really need a per-method JSON-Generator? The per-Database one is not enough?

Original comment by ff...@gmx.de on 1 Sep 2010 at 8:53

GoogleCodeExporter commented 8 years ago
Yes, i need, because i have a stateful  AbstractTypeMapper. I cannot share this 
mapper between threads.

Original comment by roman.pi...@gmail.com on 1 Sep 2010 at 9:50

GoogleCodeExporter commented 8 years ago
oh.. interesting.. what does the type mapper do?

Original comment by ff...@gmx.de on 1 Sep 2010 at 10:08

GoogleCodeExporter commented 8 years ago
Ahh sorry, i was wrong. You are right, i don't need JSON serializer per method 
(at least for now ;-), per database is enough for me.

But back to your question. Maybe it could be interesting to you. I'm using that 
stateful mapper for polymorphism when deserializing JSON to java beans.  I have 
domains objects (java beans), every domain object has associated a descriptor. 
Descriptor is something like Class in Java, it provides some introspection 
capabilities (fields, associations, inheritance etc.) about a given domain 
object.  The stateful  AbstractTypeMapper reads this descriptor at the 
beginning of JSON parsing (first call of getTypeHint) then store them as a 
instance field and use introspection for type hints.

Very short explanation by code.

public class Car {
    private Descriptor descriptor;
    private Wheel wheels;
}

public abstract class Wheel {
   private Descriptor;
}

public class WheelX extends Wheel {}
public class WheelY extends Wheel {}

Then let say i have following json:

{
  "descritpor": "Car"
  "wheels": [
      {"descriptor": "WheelX"},  {"descriptor": "WheelY"}
  ]
}

The statefull type mapper reads root descriptor and knows property paths where 
may expect next descriptors in JSON structure.  For subsequent getTypeHint 
calls these descriptors are extracted and used for type hinting.

public Class getTypeHint(JSONTokenizer tokenizer, String parsePathInfo, Class 
typeHint) { 
        if(JSON_ROOT.equals(parsePathInfo)) {
            this.descriptor  = extractDescriptor(tokenizer);
            return this.descriptor.getJavaClass();
        }

        if(this.descriptor.isChildDescriptorPath(parsePathInfo)){
            Descriptor childDescriptor = extractDescriptor(tokenizer);
            return childDescriptor.getJavaClass();
        }
                return null;        
}

Original comment by roman.pi...@gmail.com on 1 Sep 2010 at 11:57

GoogleCodeExporter commented 8 years ago
What is the goal here? Why is the normal introspection abilities of JSON / 
javascript not enough?

Original comment by ff...@gmx.de on 11 Sep 2010 at 9:14