| 1 | class News(object): pass |
|---|
| 2 | |
|---|
| 3 | def news_representer(dumper, news): |
|---|
| 4 | # skip converting a news in a dict: |
|---|
| 5 | data = { |
|---|
| 6 | 'date': '732638', |
|---|
| 7 | 'fields': { |
|---|
| 8 | '': {}, |
|---|
| 9 | 'it': {'title': 'Hello World'} |
|---|
| 10 | }, |
|---|
| 11 | 'guid': '0000010f153544cf8a314808007f000000000001', |
|---|
| 12 | 'expiration': None |
|---|
| 13 | } |
|---|
| 14 | return dumper.represent_mapping('!news', data) |
|---|
| 15 | |
|---|
| 16 | def news_constructor(loader, node): |
|---|
| 17 | nodes = loader.construct_mapping(node) |
|---|
| 18 | print nodes |
|---|
| 19 | return nodes |
|---|
| 20 | |
|---|
| 21 | import yaml |
|---|
| 22 | |
|---|
| 23 | yaml.add_representer(News, news_representer) |
|---|
| 24 | yaml.add_constructor('!news', news_constructor) |
|---|
| 25 | |
|---|
| 26 | foo = News() |
|---|
| 27 | |
|---|
| 28 | bar = yaml.load(yaml.dump(foo)) |
|---|
| 29 | print bar |
|---|