question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamically ignore members

See original GitHub issue

I’m trying to only emit certain properties as elements in certain situations. For example:

public class Leistungszeitraum
{
    public enum LeistungszeitraumTyp
    {
        Tag = 1,
        Monat
    }

    [XmlAttribute]
    public LeistungszeitraumTyp Typ { get; set; }

    public DateTime Tag { get; set; }
    public string Monat { get; set; }

    //[EditorBrowsable(EditorBrowsableState.Never)]
    //public bool ShouldSerializeTag()
    //    => Typ == LeistungszeitraumTyp.Tag;

    //[EditorBrowsable(EditorBrowsableState.Never)]
    //public bool ShouldSerializeMonat()
    //    => Typ == LeistungszeitraumTyp.Monat;
}

With classic XmlSerializer, I can accomplish this with the commented-out ShouldSerialize* members. This doesn’t appear to be supported by ExtendedXmlSerializer. Instead, this comment suggests using EmitWhen, but that doesn’t appear to work for me:

var serializer = new ConfigurationContainer()
                 .Type<Leistungszeitraum>(c =>
                 {
                     c.Member(x => x.Tag).EmitWhen(x => x.Typ == Leistungszeitraum.LeistungszeitraumTyp.Tag);
                     c.Member(x => x.Monat).EmitWhen(x => x.Typ == Leistungszeitraum.LeistungszeitraumTyp.Monat);
                 })
                 .Create();

Instead, this apparently defines whether the entire type gets emitted or not.

Do I need to use the other overload of Member()?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chuckercommented, May 14, 2018

Sorry, hadn’t gotten a chance to test it. It works as expected.

Regarding the package, I had to remove it from the local package cache and make sure the custom feed was listed before nuget.org (makes sense, but someone else may overlook this part). Is there a reason you aren’t simply giving the package its own pre-release version, like 2.1.4-issue182? That would fix the caching/precedence issues. (Does the CI not support this?)

Anyway, thanks for implementing this!

1reaction
Mike-E-angelocommented, May 8, 2018

Boh… thanks for pointing this out @chucker. It’s sort of funny that I absolutely hate the ShouldSerialize* magic string/definition conventions but I never put in a suitable replacement. 😆 It’s technically an enhancement but really it’s a bug. I will try to have something for you by EOD today to remedy this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ignore fields from Java object dynamically while sending ...
This is what you need to do to implement Dynamically ignore properties. 1) First, you need to add @JsonFilter from com. fasterxml.
Read more >
Spring Boot — Dynamically ignore fields while serializing ...
Spring Boot — Dynamically ignore fields while serializing Java Object to JSON ; Create a non-blocking REST Api using Spring @Async and Polling...
Read more >
Dynamically Ignore Fields While Serializing Java Objects ...
Learn how you can label fields to be ignored when serializing objects using annotations and Jackson.
Read more >
Jackson Ignore Properties on Marshalling
This tutorial will show how to ignore certain fields when serializing an object to JSON using Jackson 2.x.
Read more >
How to ignore properties with System.Text.Json
To ignore individual properties, use the [JsonIgnore] attribute. The following example shows a type to serialize. It also shows the JSON ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found