json.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. # Licensed under the MIT License. See LICENSE in the project root
  3. # for license information.
  4. """Improved JSON serialization.
  5. """
  6. import builtins
  7. import json
  8. import numbers
  9. import operator
  10. JsonDecoder = json.JSONDecoder
  11. class JsonEncoder(json.JSONEncoder):
  12. """Customizable JSON encoder.
  13. If the object implements __getstate__, then that method is invoked, and its
  14. result is serialized instead of the object itself.
  15. """
  16. def default(self, value):
  17. try:
  18. get_state = value.__getstate__
  19. except AttributeError:
  20. pass
  21. else:
  22. return get_state()
  23. return super().default(value)
  24. class JsonObject(object):
  25. """A wrapped Python object that formats itself as JSON when asked for a string
  26. representation via str() or format().
  27. """
  28. json_encoder_factory = JsonEncoder
  29. """Used by __format__ when format_spec is not empty."""
  30. json_encoder = json_encoder_factory(indent=4)
  31. """The default encoder used by __format__ when format_spec is empty."""
  32. def __init__(self, value):
  33. assert not isinstance(value, JsonObject)
  34. self.value = value
  35. def __getstate__(self):
  36. raise NotImplementedError
  37. def __repr__(self):
  38. return builtins.repr(self.value)
  39. def __str__(self):
  40. return format(self)
  41. def __format__(self, format_spec):
  42. """If format_spec is empty, uses self.json_encoder to serialize self.value
  43. as a string. Otherwise, format_spec is treated as an argument list to be
  44. passed to self.json_encoder_factory - which defaults to JSONEncoder - and
  45. then the resulting formatter is used to serialize self.value as a string.
  46. Example::
  47. format("{0} {0:indent=4,sort_keys=True}", json.repr(x))
  48. """
  49. if format_spec:
  50. # At this point, format_spec is a string that looks something like
  51. # "indent=4,sort_keys=True". What we want is to build a function call
  52. # from that which looks like:
  53. #
  54. # json_encoder_factory(indent=4,sort_keys=True)
  55. #
  56. # which we can then eval() to create our encoder instance.
  57. make_encoder = "json_encoder_factory(" + format_spec + ")"
  58. encoder = eval(
  59. make_encoder, {"json_encoder_factory": self.json_encoder_factory}
  60. )
  61. else:
  62. encoder = self.json_encoder
  63. return encoder.encode(self.value)
  64. # JSON property validators, for use with MessageDict.
  65. #
  66. # A validator is invoked with the actual value of the JSON property passed to it as
  67. # the sole argument; or if the property is missing in JSON, then () is passed. Note
  68. # that None represents an actual null in JSON, while () is a missing value.
  69. #
  70. # The validator must either raise TypeError or ValueError describing why the property
  71. # value is invalid, or else return the value of the property, possibly after performing
  72. # some substitutions - e.g. replacing () with some default value.
  73. def _converter(value, classinfo):
  74. """Convert value (str) to number, otherwise return None if is not possible"""
  75. for one_info in classinfo:
  76. if issubclass(one_info, numbers.Number):
  77. try:
  78. return one_info(value)
  79. except ValueError:
  80. pass
  81. def of_type(*classinfo, **kwargs):
  82. """Returns a validator for a JSON property that requires it to have a value of
  83. the specified type. If optional=True, () is also allowed.
  84. The meaning of classinfo is the same as for isinstance().
  85. """
  86. assert len(classinfo)
  87. optional = kwargs.pop("optional", False)
  88. assert not len(kwargs)
  89. def validate(value):
  90. if (optional and value == ()) or isinstance(value, classinfo):
  91. return value
  92. else:
  93. converted_value = _converter(value, classinfo)
  94. if converted_value:
  95. return converted_value
  96. if not optional and value == ():
  97. raise ValueError("must be specified")
  98. raise TypeError("must be " + " or ".join(t.__name__ for t in classinfo))
  99. return validate
  100. def default(default):
  101. """Returns a validator for a JSON property with a default value.
  102. The validator will only allow property values that have the same type as the
  103. specified default value.
  104. """
  105. def validate(value):
  106. if value == ():
  107. return default
  108. elif isinstance(value, type(default)):
  109. return value
  110. else:
  111. raise TypeError("must be {0}".format(type(default).__name__))
  112. return validate
  113. def enum(*values, **kwargs):
  114. """Returns a validator for a JSON enum.
  115. The validator will only allow the property to have one of the specified values.
  116. If optional=True, and the property is missing, the first value specified is used
  117. as the default.
  118. """
  119. assert len(values)
  120. optional = kwargs.pop("optional", False)
  121. assert not len(kwargs)
  122. def validate(value):
  123. if optional and value == ():
  124. return values[0]
  125. elif value in values:
  126. return value
  127. else:
  128. raise ValueError("must be one of: {0!r}".format(list(values)))
  129. return validate
  130. def array(validate_item=False, vectorize=False, size=None):
  131. """Returns a validator for a JSON array.
  132. If the property is missing, it is treated as if it were []. Otherwise, it must
  133. be a list.
  134. If validate_item=False, it's treated as if it were (lambda x: x) - i.e. any item
  135. is considered valid, and is unchanged. If validate_item is a type or a tuple,
  136. it's treated as if it were json.of_type(validate).
  137. Every item in the list is replaced with validate_item(item) in-place, propagating
  138. any exceptions raised by the latter. If validate_item is a type or a tuple, it is
  139. treated as if it were json.of_type(validate_item).
  140. If vectorize=True, and the value is neither a list nor a dict, it is treated as
  141. if it were a single-element list containing that single value - e.g. "foo" is
  142. then the same as ["foo"]; but {} is an error, and not [{}].
  143. If size is not None, it can be an int, a tuple of one int, a tuple of two ints,
  144. or a set. If it's an int, the array must have exactly that many elements. If it's
  145. a tuple of one int, it's the minimum length. If it's a tuple of two ints, they
  146. are the minimum and the maximum lengths. If it's a set, it's the set of sizes that
  147. are valid - e.g. for {2, 4}, the array can be either 2 or 4 elements long.
  148. """
  149. if not validate_item:
  150. validate_item = lambda x: x
  151. elif isinstance(validate_item, type) or isinstance(validate_item, tuple):
  152. validate_item = of_type(validate_item)
  153. if size is None:
  154. validate_size = lambda _: True
  155. elif isinstance(size, set):
  156. size = {operator.index(n) for n in size}
  157. validate_size = lambda value: (
  158. len(value) in size
  159. or "must have {0} elements".format(
  160. " or ".join(str(n) for n in sorted(size))
  161. )
  162. )
  163. elif isinstance(size, tuple):
  164. assert 1 <= len(size) <= 2
  165. size = tuple(operator.index(n) for n in size)
  166. min_len, max_len = (size + (None,))[0:2]
  167. validate_size = lambda value: (
  168. "must have at least {0} elements".format(min_len)
  169. if len(value) < min_len
  170. else "must have at most {0} elements".format(max_len)
  171. if max_len is not None and len(value) < max_len
  172. else True
  173. )
  174. else:
  175. size = operator.index(size)
  176. validate_size = lambda value: (
  177. len(value) == size or "must have {0} elements".format(size)
  178. )
  179. def validate(value):
  180. if value == ():
  181. value = []
  182. elif vectorize and not isinstance(value, (list, dict)):
  183. value = [value]
  184. of_type(list)(value)
  185. size_err = validate_size(value) # True if valid, str if error
  186. if size_err is not True:
  187. raise ValueError(size_err)
  188. for i, item in enumerate(value):
  189. try:
  190. value[i] = validate_item(item)
  191. except (TypeError, ValueError) as exc:
  192. raise type(exc)(f"[{repr(i)}] {exc}")
  193. return value
  194. return validate
  195. def object(validate_value=False):
  196. """Returns a validator for a JSON object.
  197. If the property is missing, it is treated as if it were {}. Otherwise, it must
  198. be a dict.
  199. If validate_value=False, it's treated as if it were (lambda x: x) - i.e. any
  200. value is considered valid, and is unchanged. If validate_value is a type or a
  201. tuple, it's treated as if it were json.of_type(validate_value).
  202. Every value in the dict is replaced with validate_value(value) in-place, propagating
  203. any exceptions raised by the latter. If validate_value is a type or a tuple, it is
  204. treated as if it were json.of_type(validate_value). Keys are not affected.
  205. """
  206. if isinstance(validate_value, type) or isinstance(validate_value, tuple):
  207. validate_value = of_type(validate_value)
  208. def validate(value):
  209. if value == ():
  210. return {}
  211. of_type(dict)(value)
  212. if validate_value:
  213. for k, v in value.items():
  214. try:
  215. value[k] = validate_value(v)
  216. except (TypeError, ValueError) as exc:
  217. raise type(exc)(f"[{repr(k)}] {exc}")
  218. return value
  219. return validate
  220. def repr(value):
  221. return JsonObject(value)
  222. dumps = json.dumps
  223. loads = json.loads