Search code examples
postgresqlentity-frameworknpgsql

Are there any drawbacks to storing unstable json data with JsonDocument DOM mapping compared to string mapping?


The Npgsql documentation specifies two options for mapping for unstable json data:

  1. String mapping (Simplest option)
public class SomeEntity
{
    public int Id { get; set; }
    [Column(TypeName = "jsonb")]
    public string Customer { get; set; }
}
  1. JsonDocument DOM mapping (Allows traversing the data)
public class SomeEntity : IDisposable
{
    public int Id { get; set; }
    public JsonDocument Customer { get; set; }

    public void Dispose() => Customer?.Dispose();
}

Are there any drawbacks to using option 2? I would imagine option 1 would technically require more data since all the " have to be escaped. Does option 2 require additional overhead from postgres to store/access compared to a string or am I overthinking this?


Solution

  • There's no difference between the two methods in what is actually stored in PostgreSQL, and option 1 does not require more data due to any escaping. In method 1, you're responsible for serializing/deserializing the JSON to a string, while in method 2 Npgsql does it for you: it simply serializes the JsonDocument to a string and sends the results to PostgreSQL. So as far as PostgreSQL is concerned, the two are equivalent.