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

Deconstruct JsonLogic for conversion into another format #668

Closed
2 tasks done
birgerj opened this issue Feb 22, 2024 · 8 comments · Fixed by #680
Closed
2 tasks done

Deconstruct JsonLogic for conversion into another format #668

birgerj opened this issue Feb 22, 2024 · 8 comments · Fixed by #680
Labels
pkg:logic question Further information is requested

Comments

@birgerj
Copy link

birgerj commented Feb 22, 2024

Documentation

  • I have consulted the documentation, and my question isn't answered there.

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:

var rule = JsonSerializer.Deserialize<Json.Logic.Rule>(jsonLogicString);
if (rule is AndRule andRule)
{
  foreach (var item in andRule.Items)
  // do stuff with items
}
else if (rule is OrRule orRule)
{
  // do stuff with orRule.Items
}

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

  • I agree to follow this project's Code of Conduct
@birgerj birgerj added the question Further information is requested label Feb 22, 2024
@gregsdennis
Copy link
Owner

gregsdennis commented Feb 25, 2024

Hey there. I think deserializing in the JsonLogic model might be overkill for your need. Have you considered just parsing into JsonNode and browsing the JSON data that way?

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 protected internal entered the picture.

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.

@gregsdennis
Copy link
Owner

I just realized you could do this as well because the .Net team made "decisions" with the JsonNode API:

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
}

@birgerj
Copy link
Author

birgerj commented Feb 26, 2024

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.
What you maybe could do instead of exposing the Item property is exposing it as an enumerator. That seems to be what the .net team did for JsonDocument as well.

@birgerj birgerj closed this as completed Feb 26, 2024
@gregsdennis
Copy link
Owner

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?

@gregsdennis
Copy link
Owner

gregsdennis commented Feb 26, 2024

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;
}

@gregsdennis gregsdennis reopened this Feb 26, 2024
@gregsdennis
Copy link
Owner

@birgerj would something like ☝️ work for you? I can push this out soon.

@birgerj
Copy link
Author

birgerj commented Mar 4, 2024

@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!

@birgerj
Copy link
Author

birgerj commented Mar 5, 2024

@gregsdennis thanks for your quick response and changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:logic question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants