1
class Person:
    first_name = superjson.Property()
    last_name = superjson.Property()
    posts = superjson.Collection(Post)

class Post:
    title = superjson.Property()
    description = superjson.Property()

# ^^^ this approach is very similar to Django models/forms

次に、次のような JSON を指定します。

{
  "first_name": "John", 
  "last_name": "Smith",
  "posts": [
    {"title": "title #1", "description": "description #1"},
    {"title": "title #2", "description": "description #2"},
    {"title": "title #3", "description": "description #3"}
  ]
}

Person内部のすべてが設定された適切なオブジェクトを構築したい:

p = superjson.deserialize(json, Person) # note, root type is explicitly provided
print p.first_name # 'John'
print p.last_name # 'Smith'
print p.posts[0].title # 'title #1'
# etc...
  • 確かに、シリアル化も容易になるはずです
  • デフォルトで ISO-8601 との間で時間をシリアル化/逆シリアル化するか、数行のコードで簡単に実現できるようにする必要があります。

だから、私はこれを探していsuperjsonます。誰かが似たようなものを見ましたか?

4

1 に答える 1

6

コランダーリモーネの組み合わせは、まさにこれを行います。

以下を使用してスキーマを定義しますcolander

import colander
import limone


@limone.content_schema
class Characteristic(colander.MappingSchema):
    id = colander.SchemaNode(colander.Int(),
                             validator=colander.Range(0, 9999))
    name = colander.SchemaNode(colander.String())
    rating = colander.SchemaNode(colander.String())        


class Characteristics(colander.SequenceSchema):
    characteristic = Characteristic()


@limone.content_schema
class Person(colander.MappingSchema):
    id = colander.SchemaNode(colander.Int(),
                             validator=colander.Range(0, 9999))
    name = colander.SchemaNode(colander.String())
    phone = colander.SchemaNode(colander.String())
    characteristics = Characteristics()


class Data(colander.SequenceSchema):
    person = Person()

次に、JSON データ構造を渡します。

deserialized = Data().deserialize(json.loads(json_string)) 
于 2012-11-03T14:09:39.913 に答える