0

I'm building a .NET WebAPI that receives Json through a Post operation. The Json that's being received could look like the following:

{
  "site": "00131231201d010231",
  "publishTime": 123123123123,
  "domains": [
    "example.com"
  ],
  "publishedBy": {
    "name": "John Doe",
    "id": "00211231201d010231"
  }
}

I converted my Json response type to C# objects which look like the following:

public class Project
{
    [Key]
    [JsonPropertyName("site")]
    public string Site { get; set; }

    [JsonPropertyName("publishTime")]
    public long PublishTime { get; set; }

    [JsonPropertyName("domains")]
    public List<Domain> Domains { get; set; }

    [JsonPropertyName("publishedBy")]
    public PublishedBy PublishedBy { get; set; }
}

public class PublishedBy
{
    [JsonPropertyName("name")]
    public string Name { get; set; }

    [Key]
    [JsonPropertyName("id")]
    public string Id { get; set; }
}

public class Domain
{
    [Key]
    public string Id { get; set; }
    public string Name { get; set; }
}

As you can see, my goal is to add the contents to my database. Only when I use List Domains, it gives me an error saying I can't use strings in EFCore when I try to add a migration.

So, I created an object called Domain. But now when I try to deserialize it gives me the following error:

System.Text.Json.JsonException: The JSON value could not be converted to spine_management.Models.Domain.

Does anyone happen to know what type I should make Domains and/or what the best way to deserialize this object is?

EDIT:

I want to keep the domains attribute, I don't want to ignore or delete them.

7
  • 1
    JsonIgnoreAttribute Commented Oct 24, 2022 at 16:41
  • @Jesse I kind of want to keep the Domains attribute, do you know a solution for that too? Commented Oct 24, 2022 at 16:46
  • domains in your JSON appears to be just an array of strings - so model it as List<string>. Commented Oct 24, 2022 at 16:55
  • @JonSkeet when I do that, I can't migrate the changes to the database Commented Oct 24, 2022 at 17:00
  • The question is quite confusing - tags claim using Json.Net, but error is from System.Text.Json; most of the post is about JSON but somehow you talk about EFCore... You may want to stop for a second and figure out what the key requirement is and rewrate the question and example around that. Note that we don't need your exact code used in your program, we need minimal reproducible example that shows the problem in the most clear way. Commented Oct 24, 2022 at 17:11

2 Answers 2

0

It's not uncommon to split between two different model structures for interacting with different infrastructure points. In this case your infrastructure points are:

  1. Deserializing JSON input
  2. Persisting data with EF

You can treat the JSON input like a "view model". It's not your core model which maps to EF database entities, but rather just an anemic DTO for deserializing data. For that model, Domains is simply a list of strings:

[JsonPropertyName("domains")]
public List<string> Domains { get; set; }

This view model is local to the application layer, not part of the core domain. Within the application logic, after deserializing the input, you can map it to the domain object. That's where you would translate the list of simple strings into a list of Domain objects. (And translate back in any output operations.)

As long as the mapping logic (which might be made simple by using tools like AutoMapper, though in this case the logic is pretty straightforward and doesn't really necessitate adding more tools) is encapsulated within that application layer, it won't pollute the rest of the domain logic.


Though it may certainly be possible to configure one or both of these tools to work together more smoothly, I often find that a simple translation layer between dependency-specific DTOs and core domain models is much simpler to build and maintain.

Sign up to request clarification or add additional context in comments.

Comments

-1

I currently use Newtonsoft JSON for serialize and deserializing. I think the reason for this error is you wrote named the string in domain "Name", but it has to be "name". Hope it works!

6 Comments

Ok, I might be answered it too quick. I just realized that u don't have to "List<Domain> Domains" because in the json version of it, it's an array of strings, not a list of an object named "domain" that has a string in it. U can try "string[] Domains" or sth like that.
Hi! Thanks for your reply! It still doesn't quite work out for me, I also saw that I forgot some code I left out for testing purposes in the Domain object, but I added it just now!
Thanks for the second reply! It still gives me this answer when I try to migrate: The property 'Project.Domains' could not be mapped because it is of type 'string[]', which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Well, did u try using "List<string>"? That json library might not be supporting an array. Try this or look at their documentation for serializing and deserializing multiple things.
Tried using List<string> too, but that gives me the same error since you can't add primitive types to a database. Tried searching this issue up everywhere for the past few hours, but I can't find anything that solves the problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.