Replies: 5 comments 2 replies
-
Testcase: public record Foo(ObjectId? NextCursor, ulong? MyValue, string? MyString);
[Test]
public void ObjectId()
{
var json ="{\"nextCursor\": null, \"myValue\": null, \"myString\": null}";
var newtonFoo = Newtonsoft.Json.JsonSerializer.Create().Deserialize<Foo>(new JsonTextReader(new StringReader(json)));
var serviceStackFoo = json.FromJson<Foo>();
Assert.That(!newtonFoo.NextCursor.HasValue);
Assert.That(!serviceStackFoo.NextCursor.HasValue);
} The NewtonSoft example works as intended. |
Beta Was this translation helpful? Give feedback.
-
Structs have scalar behavior in ServiceStack.Text, if you want DTO behavior it needs to be changed to a class or extend it to support Custom Serialization. But I'll test this behavior to see if it can be changed. |
Beta Was this translation helpful? Give feedback.
-
We can't use records with init only auto property setters in our test project, but if I expand the struct with explicit properties this test passes: [Test]
public void Does_deserialize_ObjectId()
{
var json ="{\"nextCursor\": null, \"myValue\": null, \"myString\": null}";
var foo = json.FromJson<RecordObjectId>();
Assert.That(!foo.NextCursor.HasValue);
}
public record RecordObjectId(ObjectId? NextCursor, ulong? MyValue, string MyString)
{
public ObjectId? NextCursor { get; } = NextCursor;
public ulong? MyValue { get; } = MyValue;
public string MyString { get; } = MyString;
} Can you try changing the struct impl to see if it makes a difference? |
Beta Was this translation helpful? Give feedback.
-
The example above fails however for tests where public class StructSerializationTests
{
public record Foo(ObjectId? NextCursor, ulong? MyValue, string? MyString)
{
public ObjectId? NextCursor { get; } = NextCursor;
public ulong? MyValue { get; } = MyValue;
public string? MyString { get; } = MyString;
};
[Test]
public void ObjectId_With_Value()
{
var json ="{\"nextCursor\": \"hasValue\", \"myValue\": null, \"myString\": null}";
var serviceStackFoo = json.FromJson<Foo>();
Assert.That(serviceStackFoo.NextCursor.HasValue);
}
[Test]
public void ObjectId_With_NULL_Value()
{
var json ="{\"nextCursor\": null, \"myValue\": null, \"myString\": null}";
var serviceStackFoo = json.FromJson<Foo>();
Assert.That(!serviceStackFoo.NextCursor.HasValue);
}
[Test]
public void ObjectId_Without_Value()
{
var json ="{\"myValue\": null, \"myString\": null}";
var serviceStackFoo = json.FromJson<Foo>();
Assert.That(!serviceStackFoo.NextCursor.HasValue);
}
} |
Beta Was this translation helpful? Give feedback.
-
This should now be resolved from this commit this change is available from v6.5.1+ that's now available on MyGet. |
Beta Was this translation helpful? Give feedback.
-
Hey,
I'm using ServiceStack.Text 6.5.0, C#10 and Enabled nullable reference types
I'm curious if I've missed a
JsConfig
settings or if I miss something else.I have the following record
where
ObjectId
is a readonly struct:Deserializing
var json ="{\"myValue\": null, \"myString\": null}";
results in the expected instance whereNextCursor
is null.However if the field is available (but marked with
null
)var json ="{\"nextCursor\": null, \"myValue\": null, \"myString\": null}";
theNextCursor
property has a value.I would expect it to still be null - is that a configuration that I missed? It works for the
ulong?
andstring?
example just fine.Best,
Daniel
Beta Was this translation helpful? Give feedback.
All reactions