Dump without anchors

Issue #1086 resolved
Mikael Ståldal created an issue

Dumping YAML will automatically create anchors if the same object occurs multiple times in the input. It would be good to be able to disable this feature.

Comments (20)

  1. Mikael Ståldal reporter

    Yes, and it’s reasonable to have it enabled by default. But I have a specific use case where I need to disable it.

  2. Andrey Somov

    I am nor sure about the consequences of such deviation. Defunety, it goes against the YAML spec.

    I will have a look.

  3. Andrey Somov

    is it what you expect to achieve ? I see the consequence that recursive structures cause StackOverFlow

  4. Mikael Ståldal reporter

    Yes, that’s what I want.

    And yes, I am aware of the risk of recursive structures. Ideally, it would detect recursive structures and throw some exception (before stack overflow), but it’s good enough for me if it doesn’t.

  5. Alexander Maslov

    It seems to be rather easy to do it for simple objects, but recursive ones is a problem in current implementation.
    We are not currently tracking recursion status of object references during dump as it is irrelevant when anchors are used.
    We need to check what would be an impact on code base if we want to play nicely while introducing this feature.

  6. Mikael Ståldal reporter

    Wouldn’t it be possible to use the existing anchor tracking to detect recursive objects?

    I think that a reasonable implementation would throw an exception if recursion is detected, it doesn’t need to produce any output in that case.

  7. Alexander Maslov

    feat: add dereferenceAliases to DumperOptions

    Setting this property to true will force emitter to dereference aliases: emit Node referenced by the Alias instead of Alias itself. The Anchors are not emitted also.

    Default value is true to preserve current behavior aligned with specs.

    If serializer "detects" recursive structure it fails with SerializationException.

    Implementation is on the Serialize level meaning that Nodes are always constructed with Anchors and Aliases.

    There is no test with Merge Nodes.

    see #1086

    → <<cset 64453675d543>>

  8. Alexander Maslov

    merged to master.

    Use DumberOptions.setDereferenceAliases. Remember that tree of Nodes will still have all Anchor Nodes: de-referencing happens during Serialization of the tree.

  9. Floris Westerman

    When will 2.3 likely be released? We also need this feature, so even a snapshot release would be greatly appreciated!

  10. Andrey Somov

    You can build the version.

    Unfortunately, the certificate which is required to upload to Sonatype expired. We need to create and promote new one. I will do my best to do it this summer.

  11. Floris Westerman

    Excellent, thank you so much! This saves quite some trouble manually setting up the dependencies.

  12. Log in to comment