-
Notifications
You must be signed in to change notification settings - Fork 163
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 Reference Loop #105
Comments
I think either you clean up the data from any reference loop before passing it to the view model (better imho), or you would have to override the entire serialize method:
Use the original implementation here and modify as needed. In any case, it would seem like a good idea to add that settings by default to dotNetify in the next release. |
Yep, seems like cleaning up is the better choice. |
The link was only for reference; if you go the custom serializer route, you need to provide your own implementation when overriding BaseVM Serialize method. |
@dsuryd not sure if i should open a separate issue for this. I was just wondering if it was possible to just override the casing of the serializer? I am trying to integrate this into an existing client app and all the javascript references to state are in lowerCamelCase. Is this possible without overriding the entire serializer? |
We can add similar |
Sure I would be happy to work on it. I am not 100% sure where the override would happen currently. since everything is new()'d up in something like this maybe? public sealed class VMSerializer : ISerializer, IDeserializer
{
public static JsonSerializer DefaultSerializer { get; set; } = new JsonSerializer { ContractResolver = new VMContractResolver(ignoredPropertyNames) } ;
...
public string Serialize(object viewModel, List<string> ignoredPropertyNames)
{
...
var serializer = DefaultSerializer;
...
}
} then have the extension method set the DefaultSerializer? or do you have a different idea in mind. I'm up for anything just need a little direction, thanks! |
Let's provide |
Hmm. What do we lose by overriding and not using |
VMContractResolver is not only used for that; also used for reactive properties and commands. |
did a quick test and setting the it would look something like this public class MyContractResolver : VMContractResolver
{
public MyContractResolver(List<String> ignoredPropergyNames) : base(ignoredPropergyNames)
{
NamingStrategy = new CamelCaseNamingStrategy();
}
}
...
app.UseDotNetify(config =>
{
config.UseJsonSerializerSettings(ignoredPropertyNames => new JsonSerializerSettings
{
ContractResolver = new MyContractResolver(ignoredPropertyNames)
});
}); here I have a factory method similar to /// <summary>
/// Delegate to create serializer settings.
/// </summary>
/// <param name="type">Class type.</param>
/// <param name="args">Optional constructor arguments.</param>
/// <returns>Object of type T.</returns>
public delegate JsonSerializerSettings CreateSerializerSettings(List<string> ignoredPropertyNames);
internal static CreateSerializerSettings CreateSettings = ignoredPropertyNames => new JsonSerializerSettings
{
ContractResolver = new VMContractResolver(ignoredPropertyNames)
}; then using the method on the configuration object just overrides this factory method. I appreciate any feedback! |
To make the API cleaner and simplify things, let's move the |
I'm getting ready to publish the next version update. Do you think you can submit your PR by Wednesday? |
@dsuryd I'm not sure whether to open a new issue, but I'm trying to do essentially the same thing as the OP, namely something like: JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
}; I have a database with a lot of virtual interfaces that cause this circular loop in many places, so I'd really need a global fix, is there anyway besides the |
Thanks to the PR that came out of this issue, the default serializer settings can be customized as below: app.UseDotNetify(config => {
config.UseJsonSerializerSettings(settings => {
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}
} |
@dsuryd, thanks a ton, worked like a charm! (p.s. for others you have to put |
Hi,
I'm using EFCore in my project together with dotNetify and I'm having issues with the JSON serialization.
Due to the navigation properties in my models, whenever I use them in my view models I quickly run into a reference loop.
Now, in a normal SignalR setup, I'd just configure it:
services.AddSignalR().AddJsonProtocol( options => { options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });
However, I can't manage to do this with dotNetify. Is there a way to configure the json serializer?
Regards
The text was updated successfully, but these errors were encountered: