Itâs an inexplicable issue when an editor copy-pastes content in a structured text field, trying to deserialize it for front-end usage and failing when deserializing a non-nullable Boolean as the parameter obj is NULL.
When we âclean upâ the content via copy-pasting it in notepad, the exact same content is suddenly working⌠I have tried to update the custom JsonField or Boolean serializer but to no avail.
Weâre using Strawberry Shake (v15.1.12) as the interface between DatoCMS and .NET and it fails in the following method (throwing the 1st ArgumentNullException):
private global::System.Boolean Deserialize_NonNullableBoolean(global::System.Text.Json.JsonElement ? obj) {
if (!obj.HasValue) {
throw new global::System.ArgumentNullException();
}
if (obj.Value.ValueKind == global::System.Text.Json.JsonValueKind.Null) {
throw new global::System.ArgumentNullException();
}
return\ _booleanTypeParser.Parse(obj.Value.GetBoolean() !);
}
The project is âpgb-Europeâ and a record that is still failing has the ID âVeRZaJ36TDC1BrtsjC8uygâ. I can provide you with a front-end URL when needed (itâs currently in beta).
Iâm so sorry, Iâm having a bit of trouble keeping up with this and understanding what exactly is happening. Could you perhaps please provide a step-by-step reproduction of what youâre doing so I can better follow along? (Either in text or a screen recording, etc., or even just the structured text JSON youâre trying to parse â the original, unmodified version, before you did anything to it in Notepad)
Are you saying youâre trying to copy and paste a structured text field from the admin interface directly into a .NET app as a string literal and trying to deserialize it thereâŚ? If so, I think thatâs a bad idea since thatâs an internal-use serialization meant for the UI itself. The public serialization would instead be fetching the field via the CDA or CMA APIs (which gets you clean JSON and helper utils). Our SDKs are only in JS and not .NET, but if you let me know what youâre trying to do in the app, I can try to help figure it out?
Otherwise, if Iâm totally misunderstanding, could you please provide a clearer reproduction?
Thank you and sorry about that! We donât usually use .NET, so even if this is a common workflow in that ecosystem, it is totally new to me⌠thank you for your patience
It sounds like maybe thereâs some sort of rich text or encoding that notepad might be stripping out, but which is otherwise tripping up the deserializerâŚ? But without seeing the JSON youâre trying to parse, I canât say for sure.
If itâs private, you can DM me here on the forum or email us at support@datocms.com.
Iâm unable to copy-paste the structured text JSON (âAn error occurred: Body is limited to 32000 characters; you entered 54568.â).
The steps that are taken:
the customerâs editor is copy-pasting content into a structured text field
we use Strawberry Shake as the interface between .NET and DatoCMS. This is a tool that creates full typed classes according to the schema it reads via the GraphQL query we setup for reading a content page (where the structured text was added)
Strawberry Shake tries to read the content (using a GraphQL Client) and serializes the received JSON into POCO objects
=> it fails as it tries to serialize a non-nullable boolean. There are actually booleans in the GraphQL response but these are always filled in so itâs kinda unclear why the exception actually happens. After âcleaningâ the text, it suddenly seems to work again.
PS: As I wasnât able to paste the whole JSON in here, Iâll send that via a direct e-mail.
As a quick follow-up. Maybe I should try contacting Strawberry Shake itself but maybe you (or a contact) already encountered the same issue and already know a solution to it?
Thank you! I received the JSON and checked it out.
Unfortunately, it looks totally fine to me â just our standard output, as youâd expect, and validates as correct JSON.
We donât use dotNET and arenât familiar with Strawberry Shake, unfortunately. But from the error messages, it seems like a type error. How does this client determine the GraphQL types? Is it actually doing a GraphQL introspection on your behalf and erroring out because of that, or are you manually feeding it types somehow?
I do think this is something youâll probably be able to get better support for directly from Strawberry Shake, since the JSON itself is fine.
However, hereâs a few things you could try while waiting for them to answer⌠sorry, these are really just my random guesses since itâs not a ecosystem Iâm familiar with or that we officially support:
Is Strawberry Shake Unicode-aware? You do have a few non-ASCII characters in the text, like ⢠in ⢠Geschikt voor zowel de holleâŚ. I donât know if that matters at all (maybe Notepad is implictly convering that somehow? you could do use a diff tool to see exactly what is different between our original output and the Notepad version)
Is Strawberry Shake actually able to get the correct network response back from the CDA (as in API tokens, async issues, etc.?) (It wasnât clear to me whether your Notepad test was skipping that, like if you were just copying & pasting the raw JSON vs fetching it over the network)
Maybe traverse the JSON tree and replace all the nulls (like sections[0].settings.optBackgroundImage with false or whatever non-null value that app requires)? Note that JS probably has a different truthiness model than .NET (C#?) though.
You could also try feeding it just one part of the tree at a time, e.g. one object or even property at a time, to see which one is actually erroring out
Itâs possible you might not need a special GraphQL client to fetch from the CDA? If your use case allows it, perhaps you could make a regular HTTP call to the CDA and then parse the JSON result using native JSON built-ins like System.Text.Json?
Sorry I donât have a better answer for you As far as I can tell, whatâs coming out of our API is valid and correct in this case, but something is mangling either the types or the parsing.
And just in case Iâm misunderstanding this part⌠I wanted to clarify:
The JSON you emailed us, that was without the cleaning, right? Like itâs what your client is currently erroring out on?
How exactly is Notepad being used here? Like are you saying:
AFTER you receive the broken JSON in your app, then you copy and paste it into Notepad, and then copy and paste it back into the app?
Or does the editor copy and paste the content into Notepad, and back out, before entering it into the Structured Text to begin with? (If so, what was the original content like? Could you email the original broken stuff to me too, please?)
Iâm asking because Iâm trying to ascertain if this is actually a parsing error in that client of yours, or if itâs a serialization problem on our side with certain kinds of input (the pre-Notepad original text) going into structured text in the first place.
Also, as a last resort, if this doesnât require any paid purchases (Visual Studio and Strawberry Shake are both free, right?), maybe you can make a small reproduction for us with just the relevant snippet (like the query, fetch, parsing, and error) and I can try to run it on a Windows machine to replicate the issue more clearly?
If I can do that, Iâll try my best to troubleshoot and debug it⌠but I have to warn you that itâs not something any of us on the team are very familiar with, sorry! Havenât written C# or used .NET in more than 20 yearsâŚ
I still think probably you would be able to get better support from Strawberry Shake directly, but Iâm happy to try to debug it on my side if you can share a repro with us.
Good question As far as I know, we use Strawberryâs CLI to download the schema (according to the supplied query) and it creates fully typed classes in .NET but the way of detection is kinda unclear for me and I should probably ask that directly towards Strawberryâs team.
At the start of our project, we used to serialize the content using a UTF8-writer (.NET) which broke lots of content articles but once we switched to a more general serializer, most of the content articles now work just fine except for a couple of unexplicable ones.
I have also thought about:
async issues: maybe Strawberry is streaming the response and not able to get the full payload but tries to serialize it anyway, whilst being incomplete. A question I should ask Strawberry tooâŚ
replace the nulls with false but that wouldnât explain why itâs suddenly working after copy-pasting is from notepad
The JSON I sent via e-mail was copied from the CDA Playground within DatoCMS itself so without any updates done by me or Strawberry. The âcleaningâ part was actually done by copying the text directly from the structured text editor into notepad and back. There used to be a time where editors copy-pasted content right out of Microsoft Word and that always contained lots of hidden markup and notepad was something that could clean up a lot of that stuff so it didnât break webpages. Thatâs why I looked towards Notepad as a âsolutionâ.
Iâll check with Strawberry support and let you know if anything comes up. Thanks anyway to follow along!
And we will advice the editor to copy-paste content paragraph by paragraph to see where it breaks.
A quick FYI, also for other users. I created a minimal Nuxt application and copied the GraphQL query + fragments in there, generated a TypeScript-safe client and that solution was able to render the content as it should be.
Issue seems to be situated in Strawberry and how it serializes the payload from the API.
Yeah, it sounds like a JSON deserialization issue within Strawberry itself. Wish I could help more there, sorry! But if their team has any follow-up, please let us know. Please also feel free to invite them to post here (or connect us via email at support@datocms.com) if we can help them troubleshoot this in any way.