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

Can prettyPrintType() be extend to print XML or JSON #122

Open
mungayree opened this issue Apr 5, 2018 · 2 comments
Open

Can prettyPrintType() be extend to print XML or JSON #122

mungayree opened this issue Apr 5, 2018 · 2 comments

Comments

@mungayree
Copy link

Can existing prettyPrintType be enhanced to print XML/JSON ?
python file

    """
    PersonData-Module DEFINITIONS AUTOMATIC TAGS ::=
    BEGIN
    Person-Info ::= SEQUENCE {
        person-name   [0] IMPLICIT VisibleString (SIZE(0..64)),
        person-id  [1] IMPLICIT VisibleString (SIZE(16..36)),
        person-type     [2] IMPLICIT ENUMERATED {
            e-man(0),
            e-woman(1),
            e-boy(2),
            e-girl(3),
            ...
        },
        person-last-ids    [3] IMPLICIT SEQUENCE (SIZE(1..3)) OF OCTET STRING (SIZE(4..16)),
        ...
    }
    END
    """

    # Auto-generated by asn1ate v.0.6.0 from myspec.asn
    # (last modified on 2018-04-05 10:54:16.789273)

    from pyasn1.type import univ, char, namedtype, namedval, tag, constraint, useful


    class Person_Info(univ.Sequence):
        pass


    Person_Info.componentType = namedtype.NamedTypes(
        namedtype.NamedType('person-name', char.VisibleString().subtype(subtypeSpec=constraint.ValueSizeConstraint(0, 64)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.
        namedtype.NamedType('person-id', char.VisibleString().subtype(subtypeSpec=constraint.ValueSizeConstraint(16, 36)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.t
        namedtype.NamedType('person-type', univ.Enumerated(namedValues=namedval.NamedValues(('e-man', 0), ('e-woman', 1), ('e-boy', 2), ('e-girl', 3))).subtype(implicitTag=tag.
        namedtype.NamedType('person-last-ids', univ.SequenceOf(componentType=univ.OctetString().subtype(subtypeSpec=constraint.ValueSizeConstraint(4, 16))).subtype(subtypeSpec=
    )

    p = Person_Info()
    print p.prettyPrintType()

from prettyPrintType() into XML below with some prettyPrintTypeXML() as ---
Same can be done prettyPrintTypeJSON().

<TagSet object at 0x7fd3facdde90 tags 0:32:16> -> Person_Info {
 "person-name" = <TagSet object at 0x7fd3facf1e50 tags 128:0:0> -> VisibleString
 "person-id" = <TagSet object at 0x7fd3facf1fd0 tags 128:0:1> -> VisibleString
 "person-type" = <TagSet object at 0x7fd3fa250190 tags 128:0:2> -> Enumerated
 "person-last-ids" = <TagSet object at 0x7fd3fa2504d0 tags 128:32:3> -> SequenceOf {
 <TagSet object at 0x7fd3facd6d90 tags 0:0:4> -> OctetString
 }

}
<Person_Info>
<person-name> <VisibleString/> </person-name>
<person-id> <VisibleString/> </person-id>
<person-type> <Enumerated/> </person-type>
<person-last-ids>
 <OctetString/>
</person-last-ids>

</Person_Info>
@etingof
Copy link
Owner

etingof commented Apr 7, 2018

I think it should be fairly easy to do for both XML and JSON. I may be able to look into that unless you come up with a PR. ;-)

In the grand scheme of things there will eventually be a "codec" which could turn pyasn1 object tree into JSON/XML in the same way as it currently does BER/DER/CER serialization. Because, logically, JSON/XML is just another form of data serialization.

@ahmedelhaw
Copy link

hey all, just a greedy ask, has this been implemented?

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

3 participants