pyasn1
pyasn1 copied to clipboard
Can prettyPrintType() be extend to print XML or JSON
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>
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.
hey all, just a greedy ask, has this been implemented?