Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON field ignored during load when it's an array #653

Closed
fubar-coder opened this issue Jan 18, 2021 · 3 comments
Closed

JSON field ignored during load when it's an array #653

fubar-coder opened this issue Jan 18, 2021 · 3 comments

Comments

@fubar-coder
Copy link

Test case:

using var freeSql = new FreeSql.FreeSqlBuilder()
	.UseConnectionString(DataType.Sqlite, "Data Source=C:/Spielwiese/test.sqlite")
	.UseAutoSyncStructure(true)
	.Build();

freeSql.UseJsonMap();

freeSql.CodeFirst.ConfigEntity<TestClass>(cf =>
{
	cf.Property(p => p.Name).IsNullable(false);
	cf.Property(p => p.Tags).JsonMap();
});

freeSql.Insert<TestClass>(
	new TestClass("test 1")
	{
		Tags = new[] { "a", "b" },
	})
	.ExecuteAffrows();
var records = freeSql.Queryable<TestClass>().ToList();
records.Dump();

record TestClass(string Name)
{
	public Guid Id { get; set; }
	public string[] Tags { get; init; } = Array.Empty<string>();
}

The Tags property of the loaded records is an empty record, but the data is stored correctly. I guess that the ORM sees the Tags property as a navigation property and therefore skips the JSON deserialization.

Tested with 2.0.105. The 2.3 preview throws an exception that it's unable to cast from string[] to string.

@2881099
Copy link
Collaborator

2881099 commented Jan 18, 2021

Modify Test Case:

record TestClass(string Name)
{
    public Guid Id { get; set; }
    public TestClassTagInfo Tags { get; init; }
}
record TestClassTagInfo //Important changes
{
    public string[] Tags { get; init; } = Array.Empty<string>();
}

using (var freeSql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(DataType.Sqlite, "Data Source=test11.sqlite")
    .UseAutoSyncStructure(true)
    .UseNoneCommandParameter(true)
    .UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
    .Build())
{

    freeSql.UseJsonMap();

    freeSql.CodeFirst.ConfigEntity<TestClass>(cf =>
    {
        cf.Property(p => p.Name).IsNullable(false);
        cf.Property(p => p.Tags).JsonMap();
    });

    freeSql.Insert<TestClass>(
        new TestClass("test 1")
        {
            Tags = new TestClassTagInfo { Tags = new[] { "a", "b" } },
        })
        .ExecuteAffrows();
    var records = freeSql.Queryable<TestClass>().ToList();
}

The string [] type is not supported in FreeSQL JsonMap.

It is recommended to use the new type, which can be better supported.

At present, there are some limitations in the use of jsonmap, and we will improve it later.

thank.

@fubar-coder
Copy link
Author

@luoyunchong This doesn't seem to be a viable solution, because the resulting JSON is different

2881099 added a commit that referenced this issue Aug 9, 2022
2881099 added a commit that referenced this issue Aug 9, 2022
@2881099
Copy link
Collaborator

2881099 commented Aug 9, 2022

Problem solved, v3.2.666-preview20220809

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants