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

Error handling "null" String when Feature.MINIMIZE_QUOTES is active #116

Closed
stefanleh opened this issue Jan 24, 2019 · 3 comments
Closed
Milestone

Comments

@stefanleh
Copy link

When serializing a String with content "null" and Feature.MINIMIZE_QUOTES is active the "null" value does not get quoted. Therefore when deserializing the value is not a String but null.

In com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.writeString(String) there is a special handling for boolean content like "true" and "false". Therefore id suggest to add special handling for "null" and "NULL" string values there as well.

What do you think about that?

@stefanleh
Copy link
Author

This works for me:

package com.oxaion.open.gen.model.serializer;

import java.io.IOException;
import java.io.Writer;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;

@SuppressWarnings("serial")
public class OxaionYamlFactory extends YAMLFactory {

	// Strings quoted for fun
	private final static Character STYLE_QUOTED = Character.valueOf('"');
	// Strings in literal (block) style
	private final static Character STYLE_LITERAL = Character.valueOf('|');
	private final static Character STYLE_PLAIN = null;
	
	@Override
	protected YAMLGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException {

		int feats = _yamlGeneratorFeatures;
		YAMLGenerator gen = new YAMLGenerator(ctxt, _generatorFeatures, feats, _objectCodec, out, _version) {
			
			@Override
			public void writeString(String text) throws IOException, JsonGenerationException {
			
				  if (text == null) {
						writeNull();
						return;
					}
					_verifyValueWrite("write String value");
					Character style = STYLE_QUOTED;
					if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures) && !isBooleanContent(text) && !isStringNullContent(text)) {
					  // If this string could be interpreted as a number, it must be quoted.
						if (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatFeatures)
								&& PLAIN_NUMBER_P.matcher(text).matches()) {
							style = STYLE_QUOTED;
						} else if (text.indexOf('\n') >= 0) {
							style = STYLE_LITERAL;
						} else {
							style = STYLE_PLAIN;
						}
					} else if (Feature.LITERAL_BLOCK_STYLE.enabledIn(_formatFeatures) && text.indexOf('\n') >= 0) {
						style = STYLE_LITERAL;
					}
					_writeScalar(text, "string", style);
			}
			
			private boolean isBooleanContent(String text) {
				return text.equals("true") || text.equals("false");
			}
			
			private boolean isStringNullContent(String text) {
				return text.equals("null") || text.equals("NULL");
			}


		};
		// any other initializations? No?
		return gen;
	}

}

stefanleh pushed a commit to stefanleh/jackson-dataformats-text that referenced this issue Jan 25, 2019
stefanleh pushed a commit to stefanleh/jackson-dataformats-text that referenced this issue Jan 25, 2019
@cowtowncoder
Copy link
Member

Sorry, I had missed this one. Adding on my W-I-P list. Sounds like worthy addition, yes.

@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Jun 1, 2019
cowtowncoder added a commit that referenced this issue Jun 1, 2019
@cowtowncoder
Copy link
Member

Thank you for reporting this, suggesting the fix. I manually merged fix in 2.10, will be in 2.10.0

ahgittin added a commit to ahgittin/jackson-dataformats-text that referenced this issue Mar 5, 2021
the logic added in c427465 for FasterXML#116 to force double-quoting of strings should only apply to single-line strings.
the quoting of multi-line strings look naff, and because they take literal mode, the characters aren't special and the quotes are not necessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants