Skip to content

Commit

Permalink
feat(rust, python): support deserializing struct json into df (pola-r…
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored and c-peters committed Jul 14, 2023
1 parent ca40780 commit 2d78604
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
10 changes: 1 addition & 9 deletions polars/polars-json/src/json/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,21 +378,13 @@ pub(crate) fn _deserialize<'a, A: Borrow<BorrowedValue<'a>>>(
}
}

/// Deserializes a `json` [`Value`][Value] into an [`Array`] of [`DataType`]
/// This is CPU-bounded.
/// # Error
/// This function errors iff either:
/// * `json` is not an [`Array`]
/// * `data_type` is neither [`DataType::List`] nor [`DataType::LargeList`]
///
/// [Value]: simd_json::value::Value
pub fn deserialize(json: &BorrowedValue, data_type: DataType) -> Result<Box<dyn Array>, Error> {
match json {
BorrowedValue::Array(rows) => match data_type {
DataType::LargeList(inner) => Ok(_deserialize(rows, inner.data_type)),
_ => todo!("read an Array from a non-Array data type"),
},
_ => todo!("read an Array from a non-Array JSON"),
_ => Ok(_deserialize(&[json], data_type)),
}
}

Expand Down
16 changes: 16 additions & 0 deletions py-polars/tests/unit/io/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io
import json
import typing
from typing import TYPE_CHECKING

import pytest
Expand Down Expand Up @@ -138,3 +139,18 @@ def test_json_sliced_list_serialization() -> None:
sliced_df = df[1, :]
sliced_df.write_ndjson(f)
assert f.getvalue() == b'{"col1":2,"col2":[6,7,8]}\n'


@typing.no_type_check
def test_json_deserialize_9687() -> None:
response = {
"volume": [0.0, 0.0, 0.0],
"open": [1263.0, 1263.0, 1263.0],
"close": [1263.0, 1263.0, 1263.0],
"high": [1263.0, 1263.0, 1263.0],
"low": [1263.0, 1263.0, 1263.0],
}

assert pl.read_json(json.dumps(response).encode()).to_dict(False) == {
k: [v] for k, v in response.items()
}

0 comments on commit 2d78604

Please sign in to comment.