Skip to content

Commit

Permalink
minor fix for value json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo authored and danylokravchenko committed Jan 16, 2024
1 parent 8007aba commit 7699188
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 6 additions & 8 deletions core/src/main/java/org/polypheny/db/functions/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
Expand Down Expand Up @@ -161,7 +162,7 @@ public Enumerator<Object[]> enumerator() {

private static final Pattern JSON_PATH_BASE = Pattern.compile( "^\\s*(?<mode>strict|lax)\\s+(?<spec>.+)$", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE );

private static final JsonProvider JSON_PATH_JSON_PROVIDER = new JacksonJsonProvider( PolyValue.JSON_WRAPPER );
private static final JsonProvider JSON_PATH_JSON_PROVIDER = new JacksonJsonProvider( PolyValue.JSON_WRAPPER.configure( Feature.ALLOW_UNQUOTED_FIELD_NAMES, true ) );
private static final MappingProvider JSON_PATH_MAPPING_PROVIDER = new JacksonMappingProvider( PolyValue.JSON_WRAPPER );


Expand Down Expand Up @@ -1627,8 +1628,6 @@ public static int toInt( Object o ) {
}




public static long toLong( String s ) {
if ( s.startsWith( "199" ) && s.contains( ":" ) ) {
return Timestamp.valueOf( s ).getTime();
Expand Down Expand Up @@ -1711,7 +1710,6 @@ public static BigDecimal toBigDecimal( Object o ) {
}



/**
* Helper for CAST(... AS VARCHAR(maxLength)).
*/
Expand Down Expand Up @@ -2401,9 +2399,9 @@ public static Object jsonValueExpressionExclude( PolyString input, List<PolyStri
PolyList<PolyList<PolyString>> collect = PolyList.copyOf( excluded.stream().map( e -> PolyList.of( Arrays.stream( e.value.split( "\\." ) ).map( PolyString::of ).collect( Collectors.toList() ) ) ).collect( Collectors.toList() ) );

PolyValue map = dejsonize( input );
if ( map.isMap() ){
if ( map.isMap() ) {
return rebuildMap( (PolyMap<PolyString, PolyValue>) map, collect );
}else {
} else {
return excluded.isEmpty() ? map : null;
}
} catch ( Exception e ) {
Expand Down Expand Up @@ -2484,7 +2482,7 @@ public static PathContext jsonApiCommonSyntax( PolyValue input, PolyString pathS
default -> throw Static.RESOURCE.illegalJsonPathModeInPathSpec( mode.toString(), pathSpec.value ).ex();
};
try {
return PathContext.withReturned( mode, BsonUtil.toPolyValue( BsonDocument.parse( "{key:" + ctx.read( pathWff ) + "}" ) ).asMap().get( PolyString.of( "key" ) ) );
return PathContext.withReturned( mode, PolyValue.fromJson( ctx.read( pathWff ) ) );
} catch ( Exception e ) {
return PathContext.withStrictException( e );
}
Expand Down Expand Up @@ -2605,7 +2603,7 @@ public static String toJson( PolyValue input ) {


public static PolyValue dejsonize( PolyString input ) {
return mapFromBson( BsonDocument.parse( "{ key:" + input.value + "}" ) ).asDocument().get( PolyString.of( "key" ) );
return mapFromBson( BsonDocument.parse( "{ \"key\":" + input.value + "}" ) ).asDocument().get( PolyString.of( "key" ) );
}


Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/org/polypheny/db/type/entity/PolyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
import org.apache.commons.lang3.NotImplementedException;
import org.bson.BsonDocument;
import org.bson.json.JsonParseException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.algebra.type.AlgDataType;
Expand Down Expand Up @@ -96,6 +98,7 @@
import org.polypheny.db.type.entity.temporal.PolyDate;
import org.polypheny.db.type.entity.temporal.PolyTime;
import org.polypheny.db.type.entity.temporal.PolyTimestamp;
import org.polypheny.db.util.BsonUtil;

@Value
@Slf4j
Expand Down Expand Up @@ -258,6 +261,27 @@ public static Function1<PolyValue, Object> wrapNullableIfNecessary( Function1<Po
}


public static PolyValue fromJson( String json ) {
if ( json == null ) {
return null;
} else if ( json.trim().startsWith( "{" ) ) {
return BsonUtil.toPolyValue( BsonDocument.parse( json ) );
}

try {
return BsonUtil.toPolyValue( BsonDocument.parse( "{\"key\":" + json + "}" ) ).asMap().get( PolyString.of( "key" ) );
} catch ( Throwable e ) {
log.warn( "Error on deserializing JSON." );

if ( e instanceof JsonParseException && e.getMessage().startsWith( "JSON reader was expecting a value but found '" ) ) {
return PolyString.of( json );
}
throw new GenericRuntimeException( e );
}

}


@Nullable
public static <E extends PolyValue> E fromTypedJson( String value, Class<E> clazz ) {
try {
Expand Down

0 comments on commit 7699188

Please sign in to comment.