Skip to content
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

Serializer for producing json diffs between two versions of the same object #89

Open
Kantis opened this issue Dec 6, 2023 · 4 comments

Comments

@Kantis
Copy link
Owner

Kantis commented Dec 6, 2023

From discussions in in the Kotlin serialization slack channel..

Some APIs offer / require use of PATCH inputs, where you send a PATCH request of the fields you want to change. It could be convenient if we could produce such a delta, given two instances of an object.

data class Bar(val baz: String, val b: Int)
data class Foo(val x: Int, val y: String, val z: Bar)

val first = Foo(5, "y", Bar("a", 10)) 
val second = Foo(6, "y", Bar("b", 10))

Given the above, we want to produce the json

{
  "x": 6,
  "z": {
    "baz": "b"
  }
}

I'm not entirely sure how/if we could achieve it with regular serializers to be honest.

@sschuberth
Copy link
Collaborator

I'm curious about what this is exactly about, could you add some more details to your top post? Like, where do the old and new object instances come from?

@Kantis
Copy link
Owner Author

Kantis commented Feb 23, 2024

clarified top post. let me know what you think :)

@sschuberth
Copy link
Collaborator

sschuberth commented Feb 24, 2024

Interesting, but probably a bit out-of-scope. Anyway, maybe we could have a built-in class similar to

data class Diff<T>(
    val before: T,
    val after: T
)

that, when serialized, only serializes the fields that differ in after from before. The challenge probably is to implement this without the use of reflection... I was thinking whether there's a trick to leverage the built-in string representation of the data class, but that idea was not getting me ahead.

@Kantis
Copy link
Owner Author

Kantis commented Feb 24, 2024

Oh, neat idea! I think if we use the default serializer to serialize both before and after to json-trees, then we could traverse and compare them in a JsonTransformingSerializer before emitting the final json..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants