Skip to content

Commit

Permalink
add JsonExpressionUtils.java
Browse files Browse the repository at this point in the history
  • Loading branch information
panbingkun committed Oct 12, 2024
1 parent 74ecf31 commit 8607bed
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.sql.catalyst.expressions.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import org.apache.spark.sql.catalyst.expressions.SharedFactory;
import org.apache.spark.sql.catalyst.json.CreateJacksonParser;
import org.apache.spark.sql.catalyst.util.GenericArrayData;
import org.apache.spark.unsafe.types.UTF8String;

public class JsonExpressionUtils {

public static GenericArrayData jsonObjectKeys(UTF8String json) {
// return null for `NULL` input
if (json == null) {
return null;
}
try (JsonParser jsonParser =
CreateJacksonParser.utf8String(SharedFactory.jsonFactory(), json)) {
// return null if an empty string or any other valid JSON string is encountered
if (jsonParser.nextToken() == null || jsonParser.currentToken() != JsonToken.START_OBJECT) {
return null;
}
// Parse the JSON string to get all the keys of outermost JSON object
List<UTF8String> arrayBufferOfKeys = new ArrayList<>();

// traverse until the end of input and ensure it returns valid key
while (jsonParser.nextValue() != null && jsonParser.currentName() != null) {
// add current fieldName to the ArrayBuffer
arrayBufferOfKeys.add(UTF8String.fromString(jsonParser.currentName()));

// skip all the children of inner object or array
jsonParser.skipChildren();
}
return new GenericArrayData(arrayBufferOfKeys.toArray());
} catch (IOException e) {
return null;
}
}
}

0 comments on commit 8607bed

Please sign in to comment.