In this post under Jackson Yaml, I will explain the purpose of parser feature “PARSE_BOOLEAN_LIKE_WORDS_AS_STRINGS” and how to enable or disable it.
We all know that YAML parser, when parsing a file, if it encounters word “true” or “false” it treats them as boolean.
In addition to that, synonyms of “true” and “false” like “yes” and “no” are also treated as boolean.
For example if we have the below code
Main class
1 package package1;
2
3 import com.fasterxml.jackson.core.JsonGenerationException;
4 import com.fasterxml.jackson.core.JsonToken;
5 import com.fasterxml.jackson.databind.JsonMappingException;
6 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
7 import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
8
9 import java.io.File;
10 import java.io.IOException;
11
12 public class YAMLDemo3 {
13 public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
14 File file = new File("Input2.yml");
15 YAMLFactory yamlFactory = new YAMLFactory();
16 YAMLParser yamlParser = yamlFactory.createParser(file);
17 JsonToken jsonToken = yamlParser.nextToken();
18 while(jsonToken != null) {
19 switch(jsonToken) {
20 case START_OBJECT: System.out.println("Object Started");
21 break;
22 case END_OBJECT: System.out.println("Object Ended");
23 break;
24 case START_ARRAY: System.out.println("Array Started");
25 break;
26 case END_ARRAY: System.out.println("Array Ended");
27 break;
28 case FIELD_NAME: System.out.println("Key field: " + yamlParser.getText());
29 break;
30 case VALUE_FALSE:
31 case VALUE_NULL:
32 case VALUE_NUMBER_FLOAT:
33 case VALUE_NUMBER_INT:
34 case VALUE_STRING:
35 case VALUE_TRUE:
36 default:System.out.println("Key value: " + yamlParser.getText() + " read as: " + jsonToken.name());
37 break;
38 }
39 jsonToken = yamlParser.nextToken();
40 }
41 yamlParser.close();
42 }
43 }
Using the above code, if we parse the below yml file.
Input2.yml
---
id: "1"
name: "name"
status: "NEW"
salary: yes
terminated: true
phoneNumbers:
- "phone number1"
- "phone number2"
We get the below output
Output
...
Key field: salary
Key value: yes read as: VALUE_TRUE
Key field: terminated
Key value: true read as: VALUE_TRUE
...
As you can see from the above output, the value “yes” and “true” both are treated as booleans.
This is the default behavior.
We can change this behavior of yaml parser by enabling the feature “PARSE_BOOLEAN_LIKE_WORDS_AS_STRINGS”.
Once enabled, it will treat boolean synonyms like “yes”, “no” etc as Strings.
We can enable this feature by adding the below code snippet in the main class (after line 15).
yamlFactory.enable(YAMLParser.Feature.PARSE_BOOLEAN_LIKE_WORDS_AS_STRINGS);
After adding the above code snippet and running the java code, we get the below output
Output
Key field: salary
Key value: yes read as: VALUE_STRING
Key field: terminated
Key value: true read as: VALUE_TRUE
As you can see from the above output, the value “yes” is treated as String whereas the value “true” is always treated as boolean.
In this way we can use the feature “PARSE_BOOLEAN_LIKE_WORDS_AS_STRINGS”