Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding JSON serializer class that is needed to write JSON files.
- Loading branch information
1 parent
f7a600b
commit 0090479
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
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,84 @@ | ||
JSONSerializer { | ||
classvar <>indentationLevel, <maxColums; | ||
|
||
*initClass { | ||
indentationLevel = 0; | ||
maxColums = 60; | ||
} | ||
|
||
*writeToFile{arg obj, file; | ||
var done = false; | ||
if(obj.isString and: {done.not}, { | ||
file << obj.asCompileString.("\n", JSON.nl).replace("\t", JSON.tab); | ||
done = true; | ||
}); | ||
if(obj.class === Symbol and: {done.not}, { | ||
JSONSerializer.writeToFile(obj.asString, file); | ||
done = true; | ||
}); | ||
|
||
if(obj.isKindOf(Dictionary) and: {done.not}, { | ||
JSONSerializer.indentationLevel = JSONSerializer.indentationLevel + 1; | ||
file << "{" << Char.nl << Char.tab.dup(JSONSerializer.indentationLevel).join; | ||
obj.keysValuesDo({ arg key, value, i; | ||
file << (key.asString.asCompileString ++ "\t: "); | ||
JSONSerializer.writeToFile(value, file); | ||
if((i+1) != obj.size, { | ||
file << "," << Char.nl << Char.tab.dup(JSONSerializer.indentationLevel).join; | ||
}, { | ||
JSONSerializer.indentationLevel = JSONSerializer.indentationLevel - 1; | ||
file << Char.nl << Char.tab.dup(JSONSerializer.indentationLevel).join << "}"; | ||
}); | ||
}); | ||
done = true; | ||
}); | ||
|
||
if(obj.isNil and: {done.not}, { | ||
file << "null"; | ||
done = true; | ||
}); | ||
if(obj === true and: {done.not}, { | ||
file << "true"; | ||
done = true; | ||
}); | ||
if(obj === false and: {done.not}, { | ||
file << "false"; | ||
done = true; | ||
}); | ||
if(obj.isNumber and: {done.not}, { | ||
if(obj.isNaN, { | ||
file << "NaN"; | ||
done = true; | ||
}); | ||
if(obj === inf, { | ||
file << "Infinity"; | ||
done = true; | ||
}); | ||
if(obj === (-inf), { | ||
file << "-Infinity"; | ||
done = true; | ||
}); | ||
file << obj.asString; | ||
done = true; | ||
}); | ||
if(obj.isKindOf(SequenceableCollection) and: {done.not}, { | ||
file << "["; | ||
if(JSONSerializer.getObjectStringLength(obj) > 60, { | ||
file << Char.nl << Char.tab.dup(JSONSerializer.indentationLevel + 1).join; | ||
}); | ||
obj.collect({ arg sub, i; | ||
JSONSerializer.writeToFile(sub, file); | ||
if((i+1) != obj.size, {file << ", "}); | ||
if(JSONSerializer.getObjectStringLength(obj) > 60, { | ||
file << Char.nl << Char.tab.dup(JSONSerializer.indentationLevel + 1).join; | ||
}); | ||
}); | ||
file << "]"; | ||
done = true; | ||
}); | ||
} | ||
|
||
*getObjectStringLength{arg obj; | ||
^JSON.stringify(obj).size; | ||
} | ||
} |