Skip to content

Support for tuples, namedtuples, OrderedDict and possibly others #89

Description

@davidwaroquiers

Summary

In some objects, tuples, namedtuples, OrderedDict and other data-container objects are used. Currently, MontyEncoder and MontyDecoder do not support them (it actually converts tuples to lists, OrderedDicts to dicts)

  • When decoding back these objects, they are not anymore the same type of data object and this could be a problem in some cases.
  • For OrderedDict, it might not be such of a problem as dicts are now guaranteed to be ordered in python 3.7, if I'm not wrong.

Is there any plan to implement such a feature ? Would it be useful for people ?

Example code for namedtuple

from collections import namedtuple
from monty.json import MontyEncoder
from monty.json import MontyDecoder

me = MontyEncoder()
md = MontyDecoder()

Point = namedtuple('Point', field_names=['x', 'y'])
a = Point(1, 2)

mydict = {'point': a}
mydecodeddict = md.decode(me.encode(mydict))

print(mydict == mydecodeddict) # Returns False

Suggested solution (if known)

  • Deal with these specific data objects in a similar way numpy is handled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions