-1

I know that this solution is a hack. But so far, I haven't come up with anything better for my needs.

There is a class:

public class Request 
{
    public string Name { get; set; }
    
    [JsonIgnore]
    public int Age { get; set; }
}

I need to ignore the "Age" property during deserialization, but I need it during serialization.

So far, I have only come up with using two serializers: System.Text.Json and Newtonsoft, and leaving the [JsonIgnore] attribute for the System.Text.Json serializer only. When I need to include the "Age" field, I will use Newtonsoft.

But I feel that this is very hacky, and I don't like this plan.

Why I'm resorting to a hack:

Why am I even resorting to a hack: At one point, I make a request to an external system. The external system returns the same results for identical requests (if it matters, it's a service for building routes from point A to point B, disregarding traffic). I don't want to burden the external system with unnecessary requests.
So, I've decided to cache the requests. The problem is that the "request" is an object (in this example, it's a type of Request) that comes to me in the controller. In other words, it's a new object every time, even if the contents of the request are the same.
Therefore, I decided to use a serialized string as the cache key.
There are three alternatives that came to mind.
1. Records. I'm not sure if the cache will use full comparison instead of reference comparison. But it's worth testing and not dismissing this option.
2. Caching md5. Honestly, I'm scared of collisions here. If they occur (and everything bad should happen sooner or later), then I'll be debugging the code for a long time and not understanding why I'm getting the wrong route. A partial solution would be to use more complex hashing algorithms, but then the question of performance becomes even more acute.
3. (As I was writing this question, I realized that I like this option even more than using two serializers). Cache the combination of the URL and body of the request that I want to send to the external service.

  • 1
    You may use a [custom contract resolver](https://www.newtonsoft.com/json/help/html/contractresolver.htm#CustomIContractResolverExamples) to "unignore" properties marked with `[JsonIgnore]`. See [Can I optionally turn off the JsonIgnore attribute at runtime?](https://stackoverflow.com/q/37371786). See also [Serialize Property, but Do Not Deserialize Property in Json.Net](https://stackoverflow.com/q/31731320). In fact your question looks to be a duplicate of one of those, agree? – dbc May 01 '23 at 16:12
  • Or do you prefer to use System.Text.Json instead of Json.NET? Which `[JsonIgnore]` are you applying? If `[JsonIgnore]` is from System.Text.Json then those two questions do not apply and I should not have closed this as a duplicate, and will reopen. – dbc May 01 '23 at 16:34
  • 1
    For the System.Text.Json equivalent of configuring a contract resolver to selectively ignore properties, see [Selectively exclude a property on one JSON serialization but not on another](https://stackoverflow.com/q/74928838/3744182). – dbc May 01 '23 at 17:46

1 Answers1

0

your question doesn't make any sense for me at all. But this code works for "I need to ignore the "Age" property during deserialization, but I need it during serialization."

    using Newtonsoft.Json;

    var request = new Request { Name = "John", Age = 90 };

    var json = JsonConvert.SerializeObject(request);

    request = JsonConvert.DeserializeObject<Request>(json);

public class Request
{
    public string Name { get; set; }


    public int Age { get; set; }

    [Newtonsoft.Json.JsonConstructor]
    public Request(int Age) {}
    
    public Request() { }
}
Serge
  • 40,935
  • 4
  • 18
  • 45