Skip to content

Commit

Permalink
Merge pull request #42187 from ballerina-platform/optimized-value-con…
Browse files Browse the repository at this point in the history
…version

Implement input stream parser that create anydata values
  • Loading branch information
HindujaB authored Apr 2, 2024
2 parents e71610e + e848e65 commit 4dc1323
Show file tree
Hide file tree
Showing 27 changed files with 2,701 additions and 955 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,10 @@ default Type getCachedImpliedType() {
@Deprecated
void setImmutableType(IntersectionType immutableType);

/*
* @deprecated use {@link Type#getPackage(Object, Type)} instead
*/
// TODO: https://github.com/ballerina-platform/ballerina-lang/issues/42113
@Deprecated
Module getPkg();
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
Expand All @@ -59,7 +60,7 @@
import static io.ballerina.runtime.internal.errors.ErrorReasons.VALUE_LANG_LIB_CYCLIC_VALUE_REFERENCE_ERROR;

/**
* Class {@link JsonParser} provides APIs to handle json values.
* Class {@link JsonUtils} provides APIs to handle json values.
*
* @since 2.0.0
*/
Expand All @@ -73,7 +74,7 @@ public class JsonUtils {
* @throws BError for any parsing error
*/
public static Object parse(InputStream in) throws BError {
return JsonParser.parse(in);
return JsonParser.parse(in, PredefinedTypes.TYPE_JSON);
}

/**
Expand All @@ -85,7 +86,7 @@ public static Object parse(InputStream in) throws BError {
* @throws BError for any parsing error
*/
public static Object parse(InputStream in, String charsetName) throws BError {
return JsonParser.parse(in, charsetName);
return JsonParser.parse(in, charsetName, PredefinedTypes.TYPE_JSON);
}

/**
Expand All @@ -108,7 +109,7 @@ public static Object parse(BString jsonStr) throws BError {
* @throws BError for any parsing error
*/
public static Object parse(BString jsonStr, NonStringValueProcessingMode mode) throws BError {
return JsonParser.parse(jsonStr.getValue(), mode);
return JsonParser.parse(new StringReader(jsonStr.getValue()), mode);
}

/**
Expand All @@ -131,7 +132,7 @@ public static Object parse(String jsonStr) throws BError {
* @throws BError for any parsing error
*/
public static Object parse(String jsonStr, NonStringValueProcessingMode mode) throws BError {
return JsonParser.parse(jsonStr, mode);
return JsonParser.parse(new StringReader(jsonStr), mode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import io.ballerina.runtime.api.types.AnydataType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.internal.JsonParser;
import io.ballerina.runtime.internal.ValueConverter;

import java.io.InputStream;

/**
* This class provides APIs needed for the type conversion in Ballerina.
*
Expand All @@ -44,4 +47,19 @@ private ValueUtils() {}
public static Object convert(Object value, Type targetType) throws BError {
return ValueConverter.convert(value, targetType);
}

/**
* Parses the given input stream and creates a value using a subtype of {@link AnydataType}
* given by the target type. The {@link InputStream} should only contain a sequence of characters
* that can be parsed as {@link io.ballerina.runtime.api.types.JsonType}, otherwise a {@link BError} is thrown.
* The user needs to close the {@link InputStream}.
*
* @param in input stream which contains the value content
* @param targetType target type
* @return created value
* @throws BError if the conversion fails.
*/
public static Object parse(InputStream in, Type targetType) throws BError {
return JsonParser.parse(in, targetType);
}
}
Loading

0 comments on commit 4dc1323

Please sign in to comment.