Skip to content

Unable to serialize objects using desired YAML formatting style #185

Open
@bhreinb

Description

@bhreinb

Hi there,

This is either a question or minor issue (apologies if it's the former). I'm serializing a pojo using the below code logic

public static <T> String SerializeToRawYaml(final T yamlT) {        
       final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()
                .configure(YAMLGenerator.Feature.WRITE_DOC_START_MARKER, false)
        )
                .enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
        mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
                .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
        try {
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(yamlT);
        }
        catch (JsonProcessingException e) {
            System.out.println(
                    String.format("Problem Serializing POJO Because Of Error %s", e)
            );
        }
    }

This produces sample yaml like so

name: "OuterYamlElementName"
label: "OuterYamlElementLabel"
fields:
  label: "outerYamlFieldLabel"
  value: "outerYamlFieldValue"
  isRequired: true
  toggle: "+BEYWAYS-Outer-Yaml-Field"
elements:
  name: "InnerYamlElementDepthOneName"
  label: "InnerYamlElementDepthOneLabel"
  fields:
  - label: "innerYamlFieldsDepthOneItemOneLabel"
    value: "innerYamlFieldsDepthOneItemOneValue"
    isRequired: true
    toggle: "-BEYWAYS-Inner-Yaml-Fields-Depth-One-Item-One"
  - label: "innerYamlFieldsDepthOneItemTwoLabel"
    value: "innerYamlFieldsDepthOneItemTwoValue"
    isRequired: false
  toggle: "-BEYWAYS-Inner-Yaml-Element-Depth-One"
toggle: "+BEYWAYS-Outer-Yaml-Element"

however I'd like the yaml to be generated like so

name: "OuterYamlElementName"
label: "OuterYamlElementLabel"
fields:
  - {label: "outerYamlFieldLabel", value: "outerYamlFieldValue", isRequired: true, toggle: "+BEYWAYS-Outer-Yaml-Field"}
elements:
  name: "InnerYamlElementDepthOneName"
  label: "InnerYamlElementDepthOneLabel"
  fields:
    - { label: "innerYamlFieldsDepthOneItemOneLabel", value: "innerYamlFieldsDepthOneItemOneValue", isRequired: true, toggle: "-BEYWAYS-Inner-Yaml-Fields-Depth-One-Item-One" }
    - { label: "innerYamlFieldsDepthOneItemTwoLabel", value: "innerYamlFieldsDepthOneItemTwoValue", isRequired: false }
  toggle: "-BEYWAYS-Inner-Yaml-Element-Depth-One"
toggle: "+BEYWAYS-Outer-Yaml-Element"

namely the fields items are condensed into bracket format. Is that currently supported and if so what do I need to do in order to generate that output. Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    yamlIssue related to YAML format backend

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions