Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

jsonparser.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. """
  3. The JSON import/export module.
  4. This is rudimentary for now, assuming everything is well-formed.
  5. """
  6. import sys
  7. import odml
  8. import json
  9. class OdmlSerializer(object):
  10. """
  11. converts the odml class hierarchy to dictionaries and lists
  12. """
  13. def __init__(self, odml_document):
  14. self.doc = odml_document
  15. @staticmethod
  16. def save_element(e):
  17. """
  18. returns an xml node for the odML object e
  19. """
  20. fmt = e._format
  21. cur = {'_type': fmt.__class__.__name__}
  22. # generate elements
  23. for k in fmt._args:
  24. if not hasattr(e, fmt.map(k)):
  25. continue
  26. val = getattr(e, fmt.map(k))
  27. if val is None:
  28. continue
  29. if isinstance(val, list):
  30. for v in val:
  31. ele = OdmlSerializer.save_element(v)
  32. cur.setdefault(k, []).append(ele)
  33. else:
  34. if sys.version_info < (3, 0):
  35. cur[k] = unicode(val)
  36. else:
  37. cur[k] = str(val)
  38. return cur
  39. JSON_VERSION = "1"
  40. class JSONWriter(OdmlSerializer):
  41. def __unicode__(self):
  42. doc = self.save_element(self.doc)
  43. doc['_version'] = JSON_VERSION
  44. temp = "here i am"
  45. return json.dumps(doc)
  46. def __str__(self):
  47. doc = self.save_element(self.doc)
  48. doc['_version'] = JSON_VERSION
  49. return json.dumps(doc)
  50. def write_file(self, filename):
  51. if sys.version_info < (3, 0):
  52. data = unicode(self)
  53. else:
  54. data = str(self)
  55. f = open(filename, "w")
  56. f.write(data)
  57. f.close()
  58. class OdmlReader(object):
  59. """
  60. opposite of OdmlSerializer: converts dictionaries representing
  61. odml objects back to their classes
  62. """
  63. def to_odml(self, obj):
  64. fmt = getattr(odml.format, obj['_type'])
  65. kargs = {}
  66. objects = []
  67. for k in fmt._args:
  68. v = obj.get(k, None)
  69. if isinstance(v, list):
  70. for i, nobj in enumerate(v):
  71. objects.append(self.to_odml(nobj))
  72. elif v is not None:
  73. kargs[fmt.map(k)] = v
  74. return getattr(self, "create_%s" % fmt._name)(fmt, kargs, obj, objects)
  75. def create_odML(self, fmt, kargs, obj, children):
  76. obj = fmt.create(**kargs)
  77. for i in children:
  78. obj.append(i)
  79. return obj
  80. create_section = create_odML
  81. create_value = create_odML
  82. def create_property(self, fmt, kargs, obj, children):
  83. kargs['value'] = children
  84. return self.create_odML(fmt, kargs, obj, [])
  85. class JSONReader(OdmlReader):
  86. def fromString(self, data):
  87. obj = json.loads(data)
  88. return self.to_odml(obj)
  89. def fromFile(self, infile):
  90. return self.fromString(infile.read())
  91. if __name__ == "__main__":
  92. import sys
  93. y = JSONReader().fromFile(sys.stdin)
  94. import dumper
  95. dumper.dumpDoc(y)