| 1649 | |
| 1650 | template <typename T> |
| 1651 | Status GetIntArray(const RjArray& json_array, const int32_t length, |
| 1652 | std::shared_ptr<Buffer>* out) { |
| 1653 | if (static_cast<rj::SizeType>(length) != json_array.Size()) { |
| 1654 | return Status::Invalid("Integer array had unexpected length ", json_array.Size(), |
| 1655 | " (expected ", length, ")"); |
| 1656 | } |
| 1657 | |
| 1658 | ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateBuffer(length * sizeof(T), pool_)); |
| 1659 | |
| 1660 | T* values = reinterpret_cast<T*>(buffer->mutable_data()); |
| 1661 | |
| 1662 | for (auto [i, val] : Zip(Enumerate<rj::SizeType>, json_array)) { |
| 1663 | if constexpr (sizeof(T) < sizeof(int64_t)) { |
| 1664 | DCHECK(val.IsInt() || val.IsInt64()); |
| 1665 | if (val.IsInt()) { |
| 1666 | values[i] = static_cast<T>(val.GetInt()); |
| 1667 | } else { |
| 1668 | values[i] = static_cast<T>(val.GetInt64()); |
| 1669 | } |
| 1670 | } else { |
| 1671 | // Read 64-bit integers as strings, as JSON numbers cannot represent |
| 1672 | // them exactly. |
| 1673 | DCHECK(val.IsString()); |
| 1674 | |
| 1675 | using ArrowType = typename CTypeTraits<T>::ArrowType; |
| 1676 | if (!ParseValue<ArrowType>(val.GetString(), val.GetStringLength(), &values[i])) { |
| 1677 | return Status::Invalid("Failed to parse integer: '", |
| 1678 | std::string(val.GetString(), val.GetStringLength()), |
| 1679 | "'"); |
| 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | *out = std::move(buffer); |
| 1685 | return Status::OK(); |
| 1686 | } |
| 1687 | |
| 1688 | template <typename T> |
| 1689 | Status CreateList(const std::shared_ptr<DataType>& type) { |