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

Feature/YAML support #147

Merged
merged 32 commits into from
Apr 17, 2024
Merged

Feature/YAML support #147

merged 32 commits into from
Apr 17, 2024

Conversation

harrel56
Copy link
Owner

@harrel56 harrel56 commented Feb 8, 2024

No description provided.

@harrel56 harrel56 added the enhancement New feature or request label Feb 8, 2024
@harrel56 harrel56 linked an issue Feb 8, 2024 that may be closed by this pull request
Comment on lines 75 to 77
public Factory() {
this.yaml = new Yaml(new JsonSchemaCompatibleConstructor());
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let the user configure?

Suggested change
public Factory() {
this.yaml = new Yaml(new JsonSchemaCompatibleConstructor());
}
public Factory(Yaml yaml) {
this.yaml = yaml;
}
public Factory() {
this(new Yaml(new JsonSchemaCompatibleConstructor()));
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, initially it was that way but I felt it may do no good. If it's not properly configured it won't be complaint with JSON Schema spec. Still not sure though - do you feel it'd be useful? Do you heave some specific case already?

I liked the idea that users can reuse their own Yaml instances if they want to but it requires many customizations to make it right

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For v2 I would have a use case: to share my instance, just to be consistent with normal parsing versus validator-parsing.

If it's not properly configured it won't be complaint with JSON Schema spec.

Is this because this factory also parses the Schema?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because this factory also parses the Schema?

it applies to instance and schema documents:

  • having non-string property names would break stuff
  • floating point numbers are not parsed with enough precision (all parsed to Double)

so if any document has these it will break

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ofc allowing user to modify yaml factory and giving appropriate warning is also viable solution - but I'm still not sure if that's a good idea

Comment on lines 97 to 104
AbstractConstruct constr = new AbstractConstruct() {
@Override
public Object construct(Node node) {
return new BigDecimal(((ScalarNode) node).getValue());
}
};
this.yamlConstructors.put(Tag.FLOAT, constr);
this.yamlClassConstructors.put(NodeId.scalar, new ConstructYamlStr());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind explaining in comments why these are needed? I'm not versed in JSON/schema spec. It would be purely for my own benefit, so don't bother if don't think those comments would help future maintenance.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess a java comment here can be useful. I described in previous PR comment what does this customisation achieve

Comment on lines 80 to 86
public JsonNode wrap(Object node) {
if (node instanceof SnakeYamlNode) {
return (SnakeYamlNode) node;
} else {
return new SnakeYamlNode(node);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a use case for supporting the snake YAML Node objects: I already have a Node in memory (that I loaded) and want to validate it against a schema. With this object-based validator I can't do this.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what I thought that would be a primary use case. I thought about wrapping snakeyaml nodes too but it would require conversion of nodes to Java objects. it's certainly doable but there is no exposed API for that. I checked how to achieve this and unfortunately it's not trivial, a lot of tinkering with seemingly internal lib classes. But I'm gonna revisit it

…ternal node representation rather than java objects. Now providing custom Yaml factory actually makes sense. The price is:

- no validation for duplicate keys
- no support for override (merge) syntax "<<" (plain alias/references still work)
Copy link

sonarcloud bot commented Mar 5, 2024

Copy link

sonarcloud bot commented Apr 17, 2024

@harrel56 harrel56 merged commit febe24c into master Apr 17, 2024
11 checks passed
@harrel56 harrel56 deleted the feature/yaml-provider branch April 17, 2024 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Yaml support
2 participants