-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix leading digit bug #123
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
Conversation
XML element names must not start with a digit. This fix adds "n_" to the name.
|
it should probably just throw an xmlformat exception of some kind instead of changing the element without notice. |
|
Why throw an exception, when the input is valid JSON and the desired output |
|
The choices are throwing or leaving it as is. Which shall it be? |
|
the reason to throw is that I don't want my data or keys modified without my knowledge. If the developer knows that a transition from JSON to XML is required, then choosing keys and data that fit in both models is the only proper way to go about it. Just having "magic" modify stuff is 100% wrong, |
|
The other supporting classes handle toString() validation as follows:
So at least some of the classes do some validation. Agreed that arbitrarily changing the data is not the best solution. No objection to throwing a JSONException, but it seems to me there are lots of other invalid outputs that would also need to be checked. Not sure that XML conversion is worth the effort. |
|
I think the easiest way to handle XML validation would be to use an XML writer instead of string concatenation, but that would require a bit of re-write there. I don't personally use the XML outputs, so I can't say I'd put in any effort for it. --edit |
|
I am planning to use JSONML for one of my projects, so I guess I have an interest. We already have XML.noSpace(), currently only called from JSONML.toString(). It might be reasonable to call it from XML.toString(), and refactor it to include a check for leading digits, and possibly a few other simple checks that are compatible with both classes. |
|
I see the point. But I in my case I have no control over the input. My Another solution could be a configurable replacement-pattern, with a more Andre Schlegel M: +49 175 6785374 Münzstraße 5 Sitz der Virtimo AG: Berlin 2015-05-08 17:20 GMT+02:00 Sean Leary [email protected]:
|
|
If you have a standard input specification, you could pre-process JSON before doing the XML conversion to change invalid keys. I'm not sure how large your dataset is, but for small inputs it seems reasonable. JSONObject jo = readMyInput();
// clean up invalid stuff for XML transition
if(jo.has("1key"){
// you can inline these 2 lines as well
Object val = jo.remove("1key");
jo.put("n_1key",val);
// jo.put("n_1key",jo.remove("1key"));
}
// output to xmlThis is really business rules for your application and not something that everyone would need or want. Putting this cleanup into a custom function in your application seems to be the most logical solution assuming you wanted to run with stock JSON Java implementation. If you don't mind running a modified version, I don't see a problem with your existing code change. I just don't think it fits with putting it in the core library. I do agree that some validation needs to happen, but modifying the keys even with a default/configurable value isn't a solution that should be in a shared library. |
|
Why don't simply split the XML.toString()-method into 2 separate API-methods? The first one will throw an Exception if some illegal node is found, the other will replace with given char. So the user can choose it's apropriate handling. API could look like: public String convertWithValidation(JSONObject obj) throws JSONException
public String convertWithReplacingIllegalNodeChars(JSONObject obj, char replacement) |
|
Not sure I understand; how would the app know which version to call? Let's continue this on the open pull request #138. |
Extracted hex and decimal parsing logic into separate methods to address SonarQube complexity warning: - parseHexEntity(): handles ઼ format - parseDecimalEntity(): handles &stleary#123; format This reduces cyclomatic complexity while maintaining identical functionality and all validation checks. Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Extracted hex and decimal parsing logic into separate methods to address SonarQube complexity warning: - parseHexEntity(): handles ઼ format - parseDecimalEntity(): handles &stleary#123; format This reduces cyclomatic complexity while maintaining identical functionality and all validation checks.
Extracted hex and decimal parsing logic into separate methods to address SonarQube complexity warning: - parseHexEntity(): handles ઼ format - parseDecimalEntity(): handles &stleary#123; format This reduces cyclomatic complexity while maintaining identical functionality and all validation checks.
XML element names must not start with a digit. This fix adds "n_" to the name.