Not recommended for production!
External Data Representation (XDR) is a standard data serialization format. It allows data to be transferred between different computer systems.
XDR uses a base unit of 4 bytes, serialized in big-endian order; smaller data types still occupy four bytes each after encoding. Variable-length types such as string and opaque are padded to a total divisible by four bytes. Floating-point numbers are represented in IEEE 754 format.
The xdrparser library define multiple data type, support encoding, decoding, and custom filters. This library aim to follow some semantic like the python json library (dump, dumps, load, loads).
This library aim to support xdr serialisation, not ONC RPC (Sun RPC).
As such their is no support for rpc file parsing (rpcgen).
It will be implemented as another library.
- Integer
 - Unsigned Integer
 - Boolean
 - Hyper Integer
 - Unsigned Hyper Integer
 - Floating-Point
 - Double-Precision Floating-Point
 - Fixed-Length Opaque Data
 - Variable-Length Opaque Data
 - String
 - Variable-Length String
 - Fixed-Length Array
 - Variable-Length Array
 - Structure
 - Discriminated Union
 - Void
 - Optional
 - Linked List
 - Enum
 
- Quadruple-Precision Floating-Point
 
pip3 install .from xdrparser.types import XdrStruct
from xdrparser.types import XdrInt
from xdrparser.types import XdrVariableString
class Message(XdrStruct):
    message_id = XdrInt()
    mfrom = XdrVariableString(encoding='utf-8')
    to = XdrVariableString(encoding='utf-8')
    message = XdrVariableString(encoding='utf-8')
m = Message()
value = m.dumps({"message_id": 45, "mfrom": "Foo", "to": "Bar", "message": "This is a message"})
print(value)b'\x00\x00\x00-\x00\x00\x00\x03Foo\x00\x00\x00\x00\x03Bar\x00\x00\x00\x00\x11This is a message\x00\x00\x00'from xdrparser.types import XdrInt
from xdrparser.filters import XdrFilter
class FooFilter(XdrFilter):
    def __call__(self, obj, dump=False):
        if dump:
            int(obj)
        str(obj)
foo = XdrInt(xdr_filter=FooFilter)
f.dumps("42")xdrparser is built with three main components:
- 
Types: These are classes representing the various XDR data types. They define how data is structured and its basic serialization/deserialization logic.
 - 
Filters: Filters provide a way to transform data during the serialization and deserialization process. When dumping (serializing): data flows through the filter before it's passed to the serializer. When loading (deserializing): data flows through the filter after being processed by the serializer.
 - 
Serializers: Serializers handle the low-level details of converting objects into bytes and vice-versa. They are responsible for packing and unpacking data according to the XDR standard.
 
Dumping (from object to bytes)
object -> filter -> object -> serializer -> bytes
Loading (from bytes to object)
bytes -> serializer -> object -> filter -> object