-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
Deconstruct JsonLogic for conversion into another format #668
Comments
Hey there. I think deserializing in the JsonLogic model might be overkill for your need. Have you considered just parsing into var rule = JsonNode.Parse(jsonLogicString).AsObject();
if (rule.ContainsKey("and"))
{
foreach (var item in rule["and"].AsArray())
// do stuff with items
}
else if (rule.ContainsKey("or"))
{
// do stuff with orRule.Items
} You may also want to have a read through #368 as that's the issue where the I could make the properties public, but they're supposed to be immutible, so I'd have to change the property types as well. Since they're accessible via subclasses, it'll be a breaking change. |
I just realized you could do this as well because the .Net team made "decisions" with the var rule = JsonNode.Parse(jsonLogicString);
if (rule["and"] is not null)
{
foreach (var item in rule["and"].AsArray())
// do stuff with items
}
else if (rule.ContainsKey("or"))
{
// do stuff with orRule.Items
} |
I did go that way, but I was hoping that instead of string checking I could check types. That should be more accurate and allow for stricter typed code. But I followed your suggestion and that also works. |
Thanks for the recommendation. I'll continue to play around with things to see what I can come up with. Maybe extension methods would do? |
I think this could work: public static IEnumerable<Rule> GetItems(this AndRule rule)
{
return rule.Items.AsReadOnly();
} Some of them are a bit redundant, but if you don't see the internal properties, I guess you wouldn't notice. public static Rule GetInput(this AllRule rule)
{
return rule.Input;
}
public static Rule GetRule(this AllRule rule)
{
return rule.Rule;
} |
@birgerj would something like ☝️ work for you? I can push this out soon. |
@gregsdennis yes, I could really use that! I already found a solution to my problem but this would allow me to make the code a lot more readable! |
@gregsdennis thanks for your quick response and changes! |
Documentation
Nuget Package
JsonLogic
Package Version
5.0
How can I help? Please provide as much context as possible.
I have an app that internally uses JsonLogic to describe some rules for objects.
Nog I need to convert the JsonLogic rules into another format. I was hoping to do that by deserializing the JsonLogic string and then checking the rule:
However, the
Items
properties (and other interesting properties) are all protected internal.Could these properties be made public so that the rules can be deconstructed again?
Code of Conduct
The text was updated successfully, but these errors were encountered: