Send json as a string

Can I send the JSON as a string? because when I send a JSON on the server side, the format is another.
I need to format has not changed.

[Authenticate]
[Route(“/DocumentPush”, “POST”)]
public class DocumentPush : IReturn {
public string DockId { get; set; }
public string Body { get; set; }
}

Thanks!

Don’t send complex Types as a string, it should either be a DTO, e.g:

[Authenticate] 
[Route("/DocumentPush", "POST")]
public class DocumentPush : IReturn 
{
    public string DockId { get; set; }
    public DocumentBody Body { get; set; }
}

public class DocumentBody
{
    public string str { get; set; }
}

Or if it’s untyped use a string Dictionary instead:

    public Dictionary<string,string> Body { get; set; }
1 Like

Hi Demis! Thanks for the reply!
But not all so simple ))

What I wrote is an example to make it clear what it was about, JSON can be arbitrary, the service is a hub for linking multiple systems, and what data they are transferable is unclear. Of course you can make encode and decode mode but it is not a good option.

Hi Virab, you are not sending the body value as a JSON string, you are sending a JSON object.

"Body":{"str":"987654321abc"}, would be "Body":"{\"str\":\"987654321abc\"}", if the body value should be a JSON string that happens to contain escaped JSONified data.

Unfortunately did not work.

Such data in the dictionary are not properly serializenode.

It would be ideal to have some kind of attribute for a field that is not allowed to change the incoming data.

The issue would be solved if the string can be processed so

object f() => JSON.parse(Body)

The string dictionary is for sending a dictionary of values not nested complex types. Basically what you’re trying to do is a bad idea where you’re trying to make a “hole” in your Service Contract so that you’re trying to have your Service accept anything where the serializer has no idea what Type to serialize into.

Your options are to have an object property like:

public object Body { get; set; }

Where the serialized object contains a hidden __type property to specify which C# Type to deserialize the request into, but this couples C# concerns in your payload that will fail to work in different languages and is restricted to a Runtime Whitelist due to the potential of security vulnerabilities.

Similar to this you can use JsonObject which provides some additional flexibility by allowing you to dynamically parsing an unknown JSON object:

public JsonObject Body { get; set; }

But it still isn’t ideal for nested complex types.

The other option is to keep a string Body but in this case you need to encode the JSON, i.e:

new DocumentPush {
    Body = JsonSerializer.SerializeToString(body)
}

Which will allow you to send an encoded JSON body. In this case you can use our JavaScript Utils support to then parse the JSON body into unstructured C# List / Dictionaries which does handle nested complex types.

But I’d stongly recommend avoiding sending “unknown” types in your APIs which is a source of runtime issues, has coupling to JSON and/or C# concerns, fails to work in different languages and serializers and can’t be documented in metadata services. Ideally your Services should only be using Typed DTOs but if you need to send unstructured data you can use a Dictionary of Key/Value pairs.

1 Like

Thank You Demis.
I understand that this approach has its disadvantages but these are the business requirements and can say this is the prototype.

public JsonObject Body { get; set; }: this does not work, serializes not all levels of a json document

I’ve already done with encoding, I think this is the most simple and flexible way.

Thanks for the help!