def parse_optional(p: Parser[A]) -> Parser[Sum2[None, A]]: return Parser( p.path(), Fn[JsonType, PreParsed[Sum2[None, A]]] (lambda j: fold2[PreParseError, A, PreParsed[Sum2[None, A]]] ((lambda x: fold2[PreParseError, None, PreParsed[Sum2[None, A]]] ((lambda _: F1(x), lambda x: F2(F1(x))))(parse_none(p.path( )).parse_fn()(j)), lambda x: F2(F2(x))))(p.parse_fn()(j))))
def __call__(self, a: PreParsed[A]) -> Parsed[A]: return fold2[PreParseError, A, Parsed[A]]( (fold2[List[ParseError], ParseException, Parsed[A]]((lambda x: F1( list( map( Fn[ParseError, ParseError] (lambda y: y._replace(path=self.sg.append( self.path, y.path))), x))), lambda x: F1([ParseError(self.path, x)]))), lambda x: F2(x)))(a)
def safe_parse(y: JsonDict, k: str, t: Type[A]) -> Sum2[Exception, A]: try: x = y[k] if isinstance(x, t): return F2(x) else: return F1( TypeError('Could not parse value of type ' + t.__name__ + ' from key ' + k)) except Exception as e: return F1(e)
def map_deserialize(self, j: JsonType) -> Parsed[Exception, Foo]: if not isinstance(j, JsonDict): return F1(TypeError('Expecting JSON object')) c = cast(JsonDict, j) return map2( append2(safe_parse(c, 'baz', int), safe_parse(c, 'bar', str)), lambda t: Foo(t[1], t[0]))
def x(j: JsonType) -> Parsed[JsonType]: if len(k) == 0: return F2(j) else: d = parse_dict()(j) try: aj = map2(d, lambda y: y.get(k[0])) # type: ignore ntk = k if (len(tk) == 0) else tk pj = fold2[List[ParseError], JsonType, Parsed[JsonType]]( (lambda x: F1( list( map( Fn[ParseError, ParseError] (lambda y: y._replace(error=TraverseError( k[0], y.error))), x))), lambda x: F2(x)))(ToParsed[JsonType](ntk)(bind2( aj, parse_json))) # type: ignore return bind2(pj, traverse(k[1:], ntk)) except Exception as e: return F1([ParseError(tk, UnknownParseError(e))])
def parse_json(j: str) -> Parsed[Exception, JsonType]: try: x = json.loads(j) # type: ignore if isinstance(x, dict): # type: ignore return F2(JsonDict(x)) elif isinstance(x, list): # type: ignore return F2(JsonList(x)) else: return F2(cast(JsonPrimitive, x)) except Exception as e: return F1(e)
def parse_json(x: any) -> PreParsed[JsonType]: # type: ignore try: if isinstance(x, list): # type: ignore return F2(F2(JsonList(x))) elif isinstance(x, dict): # type: ignore return F2(F3(JsonDict(x))) else: if isinstance(x, str): # type: ignore return F2(F1(F1(x))) elif isinstance(x, int): # type: ignore return F2(F1(F2(x))) elif isinstance(x, bool): # type: ignore return F2(F1(F3(x))) elif isinstance(x, type(None)): # type: ignore return F2(F1(F4(x))) else: return F1(F2(ParseException)) except Exception as e: return F1(F2(UnknownParseError(e)))
def __init__(self, fn: SerializeFn[A]) -> None: self._fn = Fn[List[A], JsonType](lambda x: F1(F4(None))) self._list_fn = fn self._path: List[str] = []
def __call__(self, j: JsonType) -> PreParsed[B]: return bind2( self._p(j), Compose( fold2[CustomParseError, B, PreParsed[B]]( (lambda x: F1(F2(x)), lambda x: F2(x))), self._c))
def deserialize(self, b: str) -> Parsed[Exception, JsonType]: try: return parse_json(b) except Exception as e: return F1(e)
def serialize_bool_fn(b: bool) -> JsonType: return F1(F3(b))
def serialize_str_fn(s: str) -> JsonType: return F1(F1(s))
def parse_list() -> ParseFn[JsonList]: err = error(JsonList) return fold3[JsonPrimitive, 'JsonList', 'JsonDict', PreParsed[JsonList]]( (lambda _: F1(err('JsonPrimitive')), lambda x: F2(x), lambda _: F1(err('Dict'))))
def bind(self, r: ListResult[A]) -> Parsed[ListResult[A]]: return F1(r[0]) if (len(r[1]) == 0 and not (len(r[0]) == 0)) else F2(r)
def __call__(self, a: Parsed[A]) -> PreParsed[A]: return fold2[List[ParseError], A, PreParsed[A]]((lambda x: F1(F1(x)), lambda x: F2(x)))(a)
def load_json(j: str) -> PreParsed[any]: # type: ignore try: return F2(json.loads(j)) # type: ignore except Exception as e: return F1(F2(UnknownParseError(e)))
def prims(t: Type[A]) -> fold4[str, int, bool, None, PreParsed[A]]: err = error(t) return fold4[str, int, bool, None, PreParsed[A]]( (lambda x: F1(err('str')), lambda x: F1(err('int')), lambda x: F1(err('bool')), lambda x: F1(err('None'))))
def parse_prim(t: Type[A], f: fold4[str, int, bool, None, PreParsed[A]]) -> ParseFn[A]: err = error(t) return fold3[JsonPrimitive, 'JsonList', 'JsonDict', PreParsed[B]]( (f, lambda x: F1(err('List')), lambda x: F1(err('Dict'))))
def serialize_int_fn(i: int) -> JsonType: return F1(F2(i))
def __init__(self) -> None: self._run = Fn[JsonType, PreParsed[H]](lambda x: F1(F2(ParseException()))) self._path = []
def serialize_none_fn(n: None) -> JsonType: return F1(F4(n))
def map_error(self, e: E) -> Parsed[E, A]: return F1(e)