Skip to content

Going to and from json #22

@alonks1234

Description

@alonks1234

Is there a way to extract json from .model_dump_json() and reconstruct / validate with .model_validate_json() ?

I am getting errors:

def array_validator(array: npt.NDArray) -> npt.NDArray:
>       if dimensions and (array_dimensions := len(array.shape)) != dimensions:
E       AttributeError: 'dict' object has no attribute 'shape'

Because the jsonified dict is not understood when validating.

        from pydantic_numpy.model import NumpyModel
        import pydantic_numpy as pnd
        from pydantic import Field
        import numpy as np
        
        class A(NumpyModel):
            numpy_arr: pnd.NpNDArrayFp16 = Field(frozen=True, strict=True, kw_only=False)
            
        a = A(numpy_arr=np.zeros(10))
        json_str = a.model_dump_json()
        a.model_validate_json(json_str)

Replication of what I would expect to be able to do

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions