Holochain Forum

What is JsonString's utility?

There is a type in crate holochain_serialization called JsonString. Here is its definition:

/// track json serialization with the rust type system!
/// JsonString wraps a string containing JSON serialized data
/// avoid accidental double-serialization or forgetting to serialize
/// serialize any type consistently including hard-to-reach places like Option<Entry> and Result
/// JsonString must not itself be serialized/deserialized
/// instead, implement and use the native `From` trait to move between types
/// - moving to/from String, str, JsonString and JsonString simply (un)wraps it as raw JSON data
/// - moving to/from any other type must offer a reliable serialization/deserialization strategy
#[derive(Debug, PartialEq, Clone, Hash, Eq, Serialize, Deserialize)]
pub struct JsonString(String);

I don’t understand why this type should exist. In my mind, you would create this type if you had functions that want to send or receive serialized JSON across their boundaries (as arguments or return vals). I don’t think you need this type within a function boundary because you could tell from the variable name whether the string contains JSON or not.

Here are some situations in which you would deal with JSON strings

  • If you have a function that reads in JSON from some interface, then you want to deserialize it immediately, before it even leaves the function boundary
  • If you have a function that outputs JSON into some interface, then you want to take a struct that has the data in the right format, and then serialize as you write out the JSON.
  • If you have a function that is agnostic to the content coming between interfaces, in other words, it just passes data through, then you’re not going to validate that it’s JSON. You probably don’t even need it to be UTF8. You want to use a Vec<u8> or the Read and Write traits.

Those are all the cases that I can think of, and in all of them, you don’t want to use a JsonString type.

Am I missing something? Is this type actually as useless as it is in my imagination?

It’s been a while since I’ve had to think about JsonString (last time was when I wrote some Dev Pulse articles back in March or so; can’t find them right now). The gist, as I understand it, is that there are so many serialisation boundaries — conductor ↔️ persistence, conductor ↔️ network transport, DNA ↔️ ribosome — that it’s useful to know when something has actually been serialised. I guess there were some issues with double-escaping in core/conductor because it wasn’t explicit whether a string value had already been escaped. But as for why that data persists in a serialised state beyond the function boundaries, I can’t say.

From activity on the repo, I get the impression that you’re heavy into core development (apologies if you’re a fellow Holo team member and I don’t recognise your handle :grimacing:). If it’s an urgent matter, I’d suggest that you reach out to David Meister for more insight.