Append a JSON value that is either an array of N elements in order or an object mapping struct names to values (omitted struct members are mapped to null).
| 745 | // or an object mapping struct names to values (omitted struct members |
| 746 | // are mapped to null). |
| 747 | Status AppendValue(sj::value& json_obj) override { |
| 748 | ARROW_ASSIGN_OR_RAISE(bool is_null, internal::IsJsonNull(json_obj)); |
| 749 | if (is_null) { |
| 750 | return this->AppendNull(); |
| 751 | } |
| 752 | sj::array array; |
| 753 | if (json_obj.get(array) == simdjson::SUCCESS) { |
| 754 | auto expected_size = static_cast<size_t>(type_->num_fields()); |
| 755 | size_t size; |
| 756 | if (array.count_elements().get(size) != simdjson::SUCCESS) { |
| 757 | return Status::Invalid("Malformed JSON array for type ", type_->ToString()); |
| 758 | } |
| 759 | if (size != expected_size) { |
| 760 | return Status::Invalid("Expected array of size ", expected_size, |
| 761 | ", got array of size ", size); |
| 762 | } |
| 763 | size_t i = 0; |
| 764 | for (auto child : array) { |
| 765 | ARROW_ASSIGN_OR_RAISE(auto child_value, |
| 766 | internal::ResolveSimdjsonResult<sj::value>( |
| 767 | child, "Could not iterate elements of JSON array: ")); |
| 768 | RETURN_NOT_OK(child_converters_[i]->AppendValue(child_value)); |
| 769 | ++i; |
| 770 | } |
| 771 | return builder_->Append(); |
| 772 | } |
| 773 | ARROW_ASSIGN_OR_RAISE(auto object, internal::GetJsonAs<sj::object>(json_obj)); |
| 774 | // Iterate the object fields in JSON order (the on-demand API is |
| 775 | // forward-only, so per-field lookups would be quadratic and would also |
| 776 | // compare against raw, still-escaped keys). Fields absent from the JSON |
| 777 | // are appended as null afterwards. |
| 778 | auto num_fields = type_->num_fields(); |
| 779 | std::vector<bool> field_seen(num_fields, false); |
| 780 | for (auto field_result : object) { |
| 781 | ARROW_ASSIGN_OR_RAISE(auto field, |
| 782 | internal::ResolveSimdjsonResult<sj::field>( |
| 783 | field_result, "Error getting field of object: ")); |
| 784 | std::string_view key; |
| 785 | if (field.unescaped_key(/*allow_replacement=*/false).get(key) != |
| 786 | simdjson::SUCCESS) { |
| 787 | return Status::Invalid("Malformed key in JSON object for type ", |
| 788 | type_->ToString()); |
| 789 | } |
| 790 | auto it = field_index_.find(key); |
| 791 | if (it == field_index_.end()) { |
| 792 | return Status::Invalid("Unexpected member \"", key, "\" in JSON object for type ", |
| 793 | type_->ToString()); |
| 794 | } |
| 795 | const int32_t field_num = it->second; |
| 796 | if (field_seen[field_num]) { |
| 797 | return Status::Invalid("Duplicate member \"", key, "\" in JSON object for type ", |
| 798 | type_->ToString()); |
| 799 | } |
| 800 | field_seen[field_num] = true; |
| 801 | sj::value value = field.value(); |
| 802 | RETURN_NOT_OK(child_converters_[field_num]->AppendValue(value)); |
| 803 | } |
| 804 | for (int32_t i = 0; i < num_fields; ++i) { |
nothing calls this directly
no test coverage detected