json icon indicating copy to clipboard operation
json copied to clipboard

Fragment function for inserting preformatted JSON string builder into document

Open lpil opened this issue 2 years ago • 6 comments

lpil avatar Aug 31 '23 17:08 lpil

Do you have any pointers or tips on how I could implement this?

I have a use case where I'm receiving some json data from elsewhere and want to insert it verbatim as a field in a larger json document, without having to round-trip decode and re-encode it.

danielytics avatar Dec 02 '24 21:12 danielytics

On Erlang it's trivial, but on JS more work is required. We would need to switch from building an object and then JSON.stringifying it to instead only JSON.stringifying strings and then building the rest of the JSON string manually. It may make sense to move the construction of the string content into Gleam when doing this rather than having the same coe on Erlang and on JS.

We would need to benchmark this against the current version to ensure we haven't unacceptably impacted performance.

lpil avatar Dec 03 '24 12:12 lpil

On Erlang, this works:

@external(erlang, "gleam_stdlib", "identity")
fn preprocessed_string(string : String) -> Json

gdotdesign avatar Apr 10 '25 08:04 gdotdesign

That's the plan! Though that is not what preprocessed means in this API. This is an unsafe cast so would probably have the word "unsafe" or "dangerously" in a long name

lpil avatar Apr 10 '25 08:04 lpil

I've run across the exact same use case, though I don't know the shape of the data so I can't do the unnecessary decode/encode even if I want to. Just leaving this comment to show desire for the feature!

ryanmiville avatar May 13 '25 13:05 ryanmiville

I got a two use cases for this.

  1. I wanted to be able to store user provided json objects, with an unknown schema, in an actor for future retrieval.
  2. I wanted to make an http endpoint that would send requests to many endpoints at once and collect their possibly json data into an array. My constraints don't allow me to just put the json responses into strings.
Why do you have these constraints? My project, DFMailbox, is made to communicate with DiamondFire (DF), a Minecraft server that lets you code with blocks. There is a very restrictive rate limit of 5 requests/150 ticks (7.5s). Because I need to be able to poll and send messages at the same time, I need a way to batch requests.

Another constraint is that even though DF parses json from responses into nice DF dictionaries, it doesn't for stringified json. Parsing json manually uses precious cpu% and hard to implement with the best json parser not being able to process spaces.

DynamicCake avatar Jun 05 '25 09:06 DynamicCake