forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74ecf31
commit 8607bed
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...yst/src/main/java/org/apache/spark/sql/catalyst/expressions/json/JsonExpressionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |