yavuztor / JsonLogic.Net

JsonLogic implementation for .Net
MIT License
50 stars 21 forks source link

Handling different naming conventions (e.g. camelCase, PascalCase) in data objects #19

Open jnnwnk opened 4 years ago

jnnwnk commented 4 years ago

We plan to use this library in a project. Since we use JavaScript on the frontend side and C# on the backend side, we stumbled over a "problem" regarding the naming conventions in each of those languages. We use camelCase on the frontend side and PascalCase on the backend side. Is there a way to use the same JSON Logic rules (all defined using camelCase or PascalCase) in on both sides, each using their own naming conventions in their data objects (existing configuration or similar)?

To illustrate it precisely with an example:

JSON Logic (using camelCase for the data properties)

{
  "==": [1, {"var":"myProperty"}]
}

Data (using PascalCase for the data object)

{
  "MyProperty": 1
}

Should evaluate to true

Our idea would be to provide a naming convention (camelCase, PascalCase, snake_case, kebab-case) to JsonLogicEvaluator and to use it to evaluate properties from the data object. Alternatively, when determining the values from the data object, in a simple case, one could simply make a case insensitive comparison of the names. Normally, this should not lead to collisions or the like, although it's possible.

If there is no solution for this issue so far, am I willing to create a PR, if this feature is also needed by other users? Are there any contribution guidelines or something like this?

jnnwnk commented 4 years ago

Since there was no feedback so far, I created a fork (for now). So far only the mapping camelCase <-> PascalCase (which we currently need) is implemented: https://github.com/jnnwnk/JsonLogic.Net

If someone is interested: Feedback is appreciated. The changes can be extended if necessary and converted into a pull request.

yavuztor commented 4 years ago

Sorry for the slow response @jnnwnk . I am having some trouble with github notifications. I will work on merging your pull request if you create one.

AieatAssam commented 4 years ago

Why not alter the data itself on the backend side prior to running the evaluator? You can use Newtonsoft contract resolver to turn your payload into the correctly named data packet matching your client? If you have data object classes, you can decorate them with the correct naming strategy?

An ugly way of doing this is to just take your data string (or object, in which case drop half of this code) and do:

var dataString = "{ \"MyProperty\": [1, 2, 3, 4] }";
var jsonIn = JToken.Parse(dataString);
dynamic objIn = JsonConvert.DeserializeObject<System.Dynamic.ExpandoObject>(dataString);
string jsonOut = JsonConvert.SerializeObject(objIn, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

Ideally you wouldn't use dynamic, of course, but have proper types.

Having the logic handle differently named data from the logic itself seems a little backwards.