Total Pageviews

2018/04/12

[snakeyaml] How to hide bean type in snakeyaml?

Problem
I try to serialize a Java object into a YAML String, the expected YAML content looks like:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  intents:
  - greet
  - inform
  - others
  - question
  entities:
  - userid
  - is_account_locked
  - pass_company_check
  actions:
  - utter_default
  - utter_greet
  - confirm:
      dependency:
        slots:
        - userid
  - unlock:
      dependency:
        slots:
        - userid
        - is_account_locked

But the actual YAML content is:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  intents:
  - greet
  - inform
  - others
  - question
  entities:
  - userid
  - is_account_locked
  - pass_company_check
  actions:
  - utter_default
  - utter_greet
  - confirm: !!test.albert.snakeyaml.SnakeYamlTest$ActionAttribute
      dependency:
        slots:
        - userid
  - unlock: !!test.albert.snakeyaml.SnakeYamlTest$ActionAttribute
      dependency:
        slots:
        - userid
        - is_account_locked

How to hide bean type in snakeyaml?


Here has sample code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  package test.albert.snakeyaml;
  
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.yaml.snakeyaml.DumperOptions;
  import org.yaml.snakeyaml.Yaml;
  import org.yaml.snakeyaml.representer.Representer;
  
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.extern.slf4j.Slf4j;
  
  @Slf4j
  public class SnakeYamlTest {
  
      public static void main(String[] args) {
          // Set DumperOptions options
          DumperOptions options = new DumperOptions();
          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
          options.setPrettyFlow(true);
            
          // Create Yaml instance with DumperOptions
          Yaml yaml = new Yaml(options);
  
          // Prepare map data for YAML
          Map<String, Object> actionMap1 = new HashMap<>();
          actionMap1.put("confirm", new ActionAttribute(new Dependency(Arrays.asList("userid"))));
  
          Map<String, Object> actionMap2 = new HashMap<>();
          actionMap2.put("unlock", new ActionAttribute(new Dependency(Arrays.asList("userid", "is_account_locked"))));
  
          Map<String, Object> map = new HashMap<>();
          map.put("intents", Arrays.asList("greet", "inform", "others", "question"));
          map.put("entities", Arrays.asList("userid", "is_account_locked", "pass_company_check"));
          map.put("actions", Arrays.asList("utter_default", "utter_greet", actionMap1, actionMap2));
  
          // Serialize a Java object into a YAML String
          String output = yaml.dump(map);
          log.info("\n" + output);
      }
  
      @Data
      @AllArgsConstructor
      private static class ActionAttribute {
          Dependency dependency;
      }
  
      @Data
      @AllArgsConstructor
      private static class Dependency {
          private List<String> slots;
      }
  }



How-To
Here has updated source code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  package test.albert.snakeyaml;
  
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.yaml.snakeyaml.DumperOptions;
  import org.yaml.snakeyaml.Yaml;
  import org.yaml.snakeyaml.nodes.Tag;
  import org.yaml.snakeyaml.representer.Representer;
  
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.extern.slf4j.Slf4j;
  
  @Slf4j
  public class SnakeYamlTest {
  
      public static void main(String[] args) {
          // Set DumperOptions options
          DumperOptions options = new DumperOptions();
          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
          options.setPrettyFlow(true);
          
          // overwrite Representer
          Representer representer = new Representer();
          representer.addClassTag(ActionAttribute.class, Tag.MAP);
  
          // Create Yaml instance with Representer and DumperOptions
          Yaml yaml = new Yaml(representer, options);
  
          // Prepare map data for YAML
          Map<String, Object> actionMap1 = new HashMap<>();
          actionMap1.put("confirm", new ActionAttribute(new Dependency(Arrays.asList("userid"))));
  
          Map<String, Object> actionMap2 = new HashMap<>();
          actionMap2.put("unlock", new ActionAttribute(new Dependency(Arrays.asList("userid", "is_account_locked"))));
  
          Map<String, Object> map = new HashMap<>();
          map.put("intents", Arrays.asList("greet", "inform", "others", "question"));
          map.put("entities", Arrays.asList("userid", "is_account_locked", "pass_company_check"));
          map.put("actions", Arrays.asList("utter_default", "utter_greet", actionMap1, actionMap2));
  
          // Serialize a Java object into a YAML String
          String output = yaml.dump(map);
          log.info("\n" + output);
      }
  
      @Data
      @AllArgsConstructor
      private static class ActionAttribute {
          Dependency dependency;
      }
  
      @Data
      @AllArgsConstructor
      private static class Dependency {
          private List<String> slots;
      }
  }

No comments: