Skip to content

LangChain integration #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 18, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add utility methods to JSONSerializer
  • Loading branch information
dhirupandey committed Jun 17, 2025
commit 4c230d746831bd235197aefb4947106534657632
23 changes: 21 additions & 2 deletions src/coherence/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import collections
import json
from abc import ABC, abstractmethod
from decimal import Decimal
from typing import Any, Callable, Dict, Final, Optional, Type, TypeVar, cast
Expand Down Expand Up @@ -70,25 +71,43 @@ def __init__(self) -> None:
self._pickler = JavaProxyPickler()
self._unpickler = JavaProxyUnpickler()

def _to_json_from_object(self, obj: object) -> str:
jsn = jsonpickle.encode(obj, context=self._pickler)
return jsn

def serialize(self, obj: object) -> bytes:
jsn: str = jsonpickle.encode(obj, context=self._pickler)
jsn: str = self._to_json_from_object(obj)
b: bytes = MAGIC_BYTE + jsn.encode()
return b

def _to_object_from_json(self, json_str: str) -> T: # type: ignore
o = jsonpickle.decode(json_str, context=self._unpickler)
return o

def deserialize(self, value: bytes) -> T: # type: ignore
if isinstance(value, bytes):
s = value.decode()
if value.__len__() == 0: # empty string
return cast(T, None)
else:
if ord(s[0]) == ord(MAGIC_BYTE):
r = jsonpickle.decode(s[1:], context=self._unpickler)
r: T = self._to_object_from_json(s[1:])
return r
else:
raise ValueError("Invalid JSON serialization format")
else:
return cast(T, value)

def flatten_to_dict(self, o: object) -> dict[Any, Any]:
jsn = self._to_json_from_object(o)
d = json.loads(jsn)
return d

def restore_to_object(self, the_dict: dict[Any, Any]) -> T: # type: ignore
jsn = json.dumps(the_dict)
o: T = self._to_object_from_json(jsn)
return o


class SerializerRegistry:
_singleton: SerializerRegistry
Expand Down