js-algorand-sdk
js-algorand-sdk copied to clipboard
3.0.0: Improve object encoding and decoding
- Upgrade to the newly released
algorand-msgpacklibrary - Remove the
from_obj_for_encodingandget_obj_for_encodingmethods. In their place, introduce the following:- An
Encodableinterface with atoEncodingDatamethod, and a staticfromEncodingDatamethod. "Encoding data" is an intermediate representation of the object in a JSMap, similar to the output of our Python SDK'sdictifymethod. - The
Encodableinterface also has agetEncodingSchemamethod and a staticencodingSchemaproperty, which return instances of the newSchemabase class. - The
Schemaclass is how "encoding data" gets transformed for encoding/decoding to/from JSON and msgpack. For example,AddressSchemaconverts anAddressclass to its 32-byte public key for encoding in msgpack, and for JSON it converts the address to its base32 string form. Specifically these translations happen in theprepareMsgpack/fromPreparedMsgpackandprepareJSON/fromPreparedJSONmethods.
- An
- Replace
EncodedSignedTransactioninterface withSignedTransactionclass
While the introduction of Schema adds some complexity, it also has these benefits:
- Consolidating encoding decisions which otherwise must be spread across every class's encoding/decoding functions
- Reducing mistakes which can be made in each implementing class. No longer do they need to worry about omitting empty values or correctly dealing with base64 encoding for JSON. The
toEncodingDataandfromEncodingDatacan be more straightforward translations now. - Real, configurable support for omitting empty values. The
NamedMapSchemaclass determines if empty values should be omitted and seamlessly restores omitted values when decoding.- Right now each map field has boolean properties
omitEmptyandrequiredwhich control different functionality.omitEmpty, if true, omits default values during encoding, whilerequiredis used for values which may or may not be present on the object.
- Right now each map field has boolean properties
- Opens up the possibility of using non-string keys, which are necessary to support blocks and stateproof transactions (to be addressed in a later PR)
This relies on generator changes as well from https://github.com/algorand/generator/pull/73
Hey @jasonpaulos, some of these look not to be backward compatible and to some extent breaking changes. Are they?
@emg110 this is still a work in progress, but yes, there are breaking change here. That's why this is going into the v3 branch