Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean custom format #4

Open
timthedevguy opened this issue Dec 19, 2024 · 3 comments
Open

Boolean custom format #4

timthedevguy opened this issue Dec 19, 2024 · 3 comments

Comments

@timthedevguy
Copy link

This is the exact package I needed for a pet project, very similar in operation to deserializing XML to C# Class so it's amazing, thank you!

I do have question, the coworker I'm writing this for likes to capitalize everything, so every single mention of true/false in the source XML is True/False. Is there anyway I can write a custom transform for this? Other than modifying the XML directly?

@timthedevguy
Copy link
Author

What I did if anyone else comes across something like this, I just used a proxy property.

export class SomeClass {
  @XmlAttribute({ name: 'Enable', type: () => String })
  private enableProxy: string = ''

  get enable(): boolean {
    return this.getBoolProxy(this.enableProxy)
  }

  set enable(value: boolean) {
    this.enableProxy = this.setBoolProxy(value)
  }

  protected getBoolProxy(value: string): boolean {
    if (value == 'True') {
      return true
    }
    return false
  }
  protected setBoolProxy(value: boolean): string {
    if (value) {
      return 'True'
    }
    return 'False'
  }
}

@Edgar-P-yan
Copy link
Owner

@timthedevguy Hi! Thanks for feature request. Your workaround with private props with setters and getters is quite good.

I actually have a prototype of what your are asking, that adds support of custom transformation of not only booleans, but for any other type too, including Date, Buffers, etc. But introducing them now would cause some inconsistencies with null/undefined/empty values serialization.

A little preview of what it will probably look like:

class CapitalizedBoolean implements CustomTransformer<boolean> {
  serialize(obj: boolean): string {
    return obj ? 'True' : 'False';
  }

  parse(chardata: string | undefined): boolean {
    return chardata == 'True' ? true : false;
  }
}

@XmlElem()
class TestTransform {
  @XmlChildElem({ transformer: new CapitalizedBoolean() })
  flag: boolean;
}

The work is in progress, i'll let you now here when a beta version comes out.

@Edgar-P-yan Edgar-P-yan reopened this Dec 20, 2024
@timthedevguy
Copy link
Author

You are awesome! Thanks for looking into the feature request. I'll keep an eye out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants