def to_json(a: Any) -> Json: return ( JsonArray(Lists.wrap(a) / to_json) if isinstance(a, (list, tuple)) else JsonObject(Map(a).valmap(to_json)) if isinstance(a, dict) else JsonNull(None) if a is None else JsonScalar(a) )
def to_json(a: 'Any') -> 'Json': # line 8 _coconut_match_to = a # line 9 _coconut_match_check = False # line 9 if _coconut.isinstance(_coconut_match_to, (list, tuple)): # line 9 a = _coconut_match_to # line 9 _coconut_match_check = True # line 9 if _coconut_match_check: # line 9 result = JsonArray(Lists.wrap(a) / to_json) # line 11 if not _coconut_match_check: # line 12 if _coconut.isinstance(_coconut_match_to, dict): # line 12 a = _coconut_match_to # line 12 _coconut_match_check = True # line 12 if _coconut_match_check: # line 12 result = JsonObject(Map(a).valmap(to_json)) # line 13 if not _coconut_match_check: # line 14 a = _coconut_match_to # line 14 _coconut_match_check = True # line 14 if _coconut_match_check: # line 14 result = JsonScalar(a) # line 15 return result # line 16
def encode(self, a: Maybe[A]) -> Either[JsonError, Json]: return Right(JsonScalar(a | None))
def encode(self, a: Union[Number, str, None]) -> Either[JsonError, Json]: return Right(JsonScalar(a))
def json_type(tpe: Type) -> Json: mod = '__builtins__' if tpe.__module__ == 'builtins' else tpe.__module__ names = Lists.split(qualname(tpe), '.').map(JsonScalar) return JsonObject(Map(module=JsonScalar(mod), names=JsonArray(names)))
def encode(self, a: Boolean) -> Either[JsonError, Json]: return Right(JsonScalar(a.value))
def encode(self, a: Path) -> Either[JsonError, Json]: return Right(JsonScalar(str(a)))
def json_type(tpe: Type) -> Json: mod = '__builtins__' if tpe.__module__ == 'builtins' else tpe.__module__ return JsonScalar(f'{mod}.{tpe.__name__}')
def encode(self, a: Sub) -> Generator: jsons = yield a._dat__values.traverse(encode_json, Either) yield Right( JsonObject( Map(a._dat__names.zip(jsons)).cat( ('__type__', JsonScalar(qualified_type(type(a)))))))