Skip to content

[JSONSerialization] Add ability to emit 'null' value. #14258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions include/swift/Basic/JSONSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ struct ScalarTraits {
};


/// This class should be specialized by any type that can be 'null' in JSON.
/// For example:
///
/// template<>
/// struct NullableTraits<MyType *> > {
/// static bool isNull(MyType *&ptr) {
/// return !ptr;
/// }
/// static MyType &get(MyType *&ptr) {
/// return *ptr;
/// }
/// };
template<typename T>
struct NullableTraits {
// Must provide:
//
// Function to return true if the value is 'null'.
// static bool isNull(const T &Val);
//
// Function to return a reference to the unwrapped value.
// static T::value_type &get(const T &Val);
};


/// This class should be specialized by any type that needs to be converted
/// to/from a JSON array. For example:
///
Expand Down Expand Up @@ -249,6 +273,23 @@ template<typename T>
struct has_ArrayTraits : public std::integral_constant<bool,
has_ArrayMethodTraits<T>::value > { };

// Test if NullableTraits<T> is defined on type T.
template <class T>
struct has_NullableTraits
{
typedef bool (*Signature_isNull)(T&);

template <typename U>
static char test(SameType<Signature_isNull, &U::isNull> *);

template <typename U>
static double test(...);

public:
static bool const value =
(sizeof(test<NullableTraits<T>>(nullptr)) == 1);
};

inline bool isNumber(StringRef S) {
static const char DecChars[] = "0123456789";
if (S.find_first_not_of(DecChars) == StringRef::npos)
Expand Down Expand Up @@ -285,6 +326,7 @@ struct missingTraits : public std::integral_constant<bool,
!has_ScalarEnumerationTraits<T>::value
&& !has_ScalarBitSetTraits<T>::value
&& !has_ScalarTraits<T>::value
&& !has_NullableTraits<T>::value
&& !has_ObjectTraits<T>::value
&& !has_ArrayTraits<T>::value> {};

Expand Down Expand Up @@ -338,6 +380,7 @@ class Output {
void endBitSetScalar();

void scalarString(StringRef &, bool);
void null();

template <typename T>
void enumCase(T &Val, const char* Str, const T ConstVal) {
Expand Down Expand Up @@ -579,6 +622,16 @@ jsonize(Output &out, T &Val, bool) {
}


template<typename T>
typename std::enable_if<has_NullableTraits<T>::value,void>::type
jsonize(Output &out, T &Obj, bool) {
if (NullableTraits<T>::isNull(Obj))
out.null();
else
jsonize(out, NullableTraits<T>::get(Obj), true);
}


template<typename T>
typename std::enable_if<validatedObjectTraits<T>::value, void>::type
jsonize(Output &out, T &Val, bool) {
Expand Down
27 changes: 14 additions & 13 deletions lib/Basic/JSONSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@ using namespace swift;
unsigned Output::beginArray() {
StateStack.push_back(ArrayFirstValue);
Stream << '[';
if (PrettyPrint) {
Stream << '\n';
}
return 0;
}

bool Output::preflightElement(unsigned, void *&) {
if (StateStack.back() != ArrayFirstValue) {
assert(StateStack.back() == ArrayOtherValue && "We must be in a sequence!");
Stream << ',';
if (PrettyPrint)
Stream << '\n';
}
if (PrettyPrint)
if (PrettyPrint) {
Stream << '\n';
indent();
}
return true;
}

Expand All @@ -46,8 +43,9 @@ void Output::postflightElement(void*) {
}

void Output::endArray() {
bool HadContent = StateStack.back() != ArrayFirstValue;
StateStack.pop_back();
if (PrettyPrint) {
if (PrettyPrint && HadContent) {
Stream << '\n';
indent();
}
Expand All @@ -66,13 +64,12 @@ bool Output::canElideEmptyArray() {
void Output::beginObject() {
StateStack.push_back(ObjectFirstKey);
Stream << "{";
if (PrettyPrint)
Stream << '\n';
}

void Output::endObject() {
bool HadContent = StateStack.back() != ObjectFirstKey;
StateStack.pop_back();
if (PrettyPrint) {
if (PrettyPrint && HadContent) {
Stream << '\n';
indent();
}
Expand All @@ -86,11 +83,11 @@ bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
if (StateStack.back() != ObjectFirstKey) {
assert(StateStack.back() == ObjectOtherKey && "We must be in an object!");
Stream << ',';
if (PrettyPrint)
Stream << '\n';
}
if (PrettyPrint)
if (PrettyPrint) {
Stream << '\n';
indent();
}
Stream << '"' << Key << "\":";
if (PrettyPrint)
Stream << ' ';
Expand Down Expand Up @@ -222,6 +219,10 @@ void Output::scalarString(StringRef &S, bool MustQuote) {
Stream << S;
}

void Output::null() {
Stream << "null";
}

void Output::indent() {
Stream.indent(StateStack.size() * 2);
}
Expand Down
100 changes: 25 additions & 75 deletions test/Syntax/Inputs/serialize_multiple_decls.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@
"layout": [
{
"kind": "AttributeList",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
"kind": "DeclModifier",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
Expand Down Expand Up @@ -65,9 +61,7 @@
"kind": "identifier",
"text": "A"
},
"leadingTrivia": [

],
"leadingTrivia": [],
"trailingTrivia": [
{
"kind": "Space",
Expand All @@ -78,23 +72,17 @@
},
{
"kind": "GenericParameterClause",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
"kind": "TypeInheritanceClause",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
"kind": "GenericWhereClause",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
Expand All @@ -104,19 +92,13 @@
"tokenKind": {
"kind": "l_brace"
},
"leadingTrivia": [

],
"trailingTrivia": [

],
"leadingTrivia": [],
"trailingTrivia": [],
"presence": "Present"
},
{
"kind": "DeclList",
"layout": [

],
"layout": [],
"presence": "Present"
},
{
Expand All @@ -129,9 +111,7 @@
"value": 1
}
],
"trailingTrivia": [

],
"trailingTrivia": [],
"presence": "Present"
}
],
Expand All @@ -144,12 +124,8 @@
"tokenKind": {
"kind": "semi"
},
"leadingTrivia": [

],
"trailingTrivia": [

],
"leadingTrivia": [],
"trailingTrivia": [],
"presence": "Missing"
}
],
Expand All @@ -163,16 +139,12 @@
"layout": [
{
"kind": "AttributeList",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
"kind": "DeclModifier",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
Expand All @@ -198,9 +170,7 @@
"kind": "identifier",
"text": "B"
},
"leadingTrivia": [

],
"leadingTrivia": [],
"trailingTrivia": [
{
"kind": "Space",
Expand All @@ -211,23 +181,17 @@
},
{
"kind": "GenericParameterClause",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
"kind": "TypeInheritanceClause",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
"kind": "GenericWhereClause",
"layout": [

],
"layout": [],
"presence": "Missing"
},
{
Expand All @@ -237,19 +201,13 @@
"tokenKind": {
"kind": "l_brace"
},
"leadingTrivia": [

],
"trailingTrivia": [

],
"leadingTrivia": [],
"trailingTrivia": [],
"presence": "Present"
},
{
"kind": "DeclList",
"layout": [

],
"layout": [],
"presence": "Present"
},
{
Expand All @@ -262,9 +220,7 @@
"value": 1
}
],
"trailingTrivia": [

],
"trailingTrivia": [],
"presence": "Present"
}
],
Expand All @@ -277,12 +233,8 @@
"tokenKind": {
"kind": "semi"
},
"leadingTrivia": [

],
"trailingTrivia": [

],
"leadingTrivia": [],
"trailingTrivia": [],
"presence": "Missing"
}
],
Expand All @@ -307,9 +259,7 @@
"value": 1
}
],
"trailingTrivia": [

],
"trailingTrivia": [],
"presence": "Present"
}
],
Expand Down
Loading