Browse Source

Change some JSON API's printing methods

pull/8/head
Anton Tarasenko 4 years ago
parent
commit
b0147bc52d
  1. 38
      sources/Text/JSON/JSONAPI.uc
  2. 22
      sources/Text/TextAPI.uc

38
sources/Text/JSON/JSONAPI.uc

@ -28,7 +28,7 @@ class JSONAPI extends AcediaObject
// complex value // complex value
var const int TNULL, TTRUE, TFALSE, TDOT, TEXPONENT; var const int TNULL, TTRUE, TFALSE, TDOT, TEXPONENT;
var const int TOPEN_BRACKET, TCLOSE_BRACKET, TOPEN_BRACE, TCLOSE_BRACE; var const int TOPEN_BRACKET, TCLOSE_BRACKET, TOPEN_BRACE, TCLOSE_BRACE;
var const int TCOMMA, TCOLON; var const int TCOMMA, TCOLON, TQUOTE;
var const int CODEPOINT_BACKSPACE, CODEPOINT_TAB, CODEPOINT_LINE_FEED; var const int CODEPOINT_BACKSPACE, CODEPOINT_TAB, CODEPOINT_LINE_FEED;
var const int CODEPOINT_FORM_FEED, CODEPOINT_CARRIAGE_RETURN; var const int CODEPOINT_FORM_FEED, CODEPOINT_CARRIAGE_RETURN;
@ -815,7 +815,7 @@ public final function MutableText Print(AcediaObject toPrint)
} }
if ( toPrint.class == class'Text' if ( toPrint.class == class'Text'
|| toPrint.class == class'MutableText') { || toPrint.class == class'MutableText') {
return _.text.FromStringM(DisplayText(Text(toPrint))); return DisplayText(Text(toPrint));
} }
if (toPrint.class == class'DynamicArray') { if (toPrint.class == class'DynamicArray') {
return PrintArray(DynamicArray(toPrint)); return PrintArray(DynamicArray(toPrint));
@ -905,7 +905,7 @@ public final function MutableText PrintObject(AssociativeArray toPrint)
nextValue = iter.Get(); nextValue = iter.Get();
if (nextKey == none) continue; if (nextKey == none) continue;
if (nextKey.class != class'Text') continue; if (nextKey.class != class'Text') continue;
printedKey = _.text.FromStringM(DisplayText(nextKey)); printedKey = DisplayText(nextKey);
printedValue = Print(nextValue); printedValue = Print(nextValue);
result.Append(printedKey).Append(T(default.TCOLON)); result.Append(printedKey).Append(T(default.TCOLON));
printedKey.FreeSelf(); printedKey.FreeSelf();
@ -923,31 +923,35 @@ public final function MutableText PrintObject(AssociativeArray toPrint)
return result; return result;
} }
// Auxiliary method to convert `Text` into it's JSON `string` // Auxiliary method to convert `Text` into it's JSON "string"
// representation. // representation.
// We can't just dump `original`'s contents into JSON output as is, // We can't just dump `original`'s contents into JSON output as is,
// since we have to replace several special characters with escaped sequences. // since we have to replace several special characters with escaped sequences.
// TODO: replace this with a more sensible solution later private final function MutableText DisplayText(Text original)
private final function string DisplayText(Text original)
{ {
local int i, length; local int i, length;
local string result; local MutableText result;
local int nextCodePoint; local Text.Character nextCharacter;
local Text.Character reverseSolidus;
reverseSolidus = _.text.CharacterFromCodePoint(CODEPOINT_REVERSE_SOLIDUS);
result = T(TQUOTE).MutableCopy();
length = original.GetLength(); length = original.GetLength();
for (i = 0; i < length; i += 1) for (i = 0; i < length; i += 1)
{ {
nextCodePoint = original.GetCharacter(i).codePoint; nextCharacter = original.GetCharacter(i);
if (DoesNeedEscaping(nextCodePoint)) if (DoesNeedEscaping(nextCharacter.codePoint))
{ {
result $= Chr(CODEPOINT_REVERSE_SOLIDUS); result.AppendCharacter(reverseSolidus);
nextCodePoint = GetEscapedVersion(nextCodePoint); nextCharacter.codePoint =
result $= Chr(nextCodePoint); GetEscapedVersion(nextCharacter.codePoint);
result.AppendCharacter(nextCharacter);
} }
else { else {
result $= Chr(nextCodePoint); result.AppendCharacter(nextCharacter);
} }
} }
return "\"" $ result $ "\""; result.Append(T(TQUOTE));
return result;
} }
// Checks whether a certain character (code point) needs to be replaced for // Checks whether a certain character (code point) needs to be replaced for
@ -1014,6 +1018,8 @@ defaultproperties
stringConstants(9) = "," stringConstants(9) = ","
TCOLON = 10 TCOLON = 10
stringConstants(10) = ":" stringConstants(10) = ":"
TQUOTE = 11
stringConstants(11) = "\""
CODEPOINT_BACKSPACE = 8 CODEPOINT_BACKSPACE = 8
CODEPOINT_TAB = 9 CODEPOINT_TAB = 9

22
sources/Text/TextAPI.uc

@ -219,6 +219,28 @@ public final function bool IsQuotationMark(Text.Character character)
return false; return false;
} }
/**
* Creates a new character from a provided code point.
*
* @param codePoint Code point that defines resulting character.
* Must be a valid Unicode code point.
* @param formatting Optional parameter that allows to specify resulting
* character's formatting. By default uses default formatting
* (text is not colored).
* @return Character defined by provided code point and, optionally,
* `formatting`.
*/
// TODO: Validity checks fro non-negative input code points
public final function Text.Character CharacterFromCodePoint(
int codePoint,
optional Text.Formatting formatting)
{
local Text.Character result;
result.codePoint = codePoint;
result.formatting = formatting;
return result;
}
/** /**
* Extracts a character at position `position` from a given plain `string`. * Extracts a character at position `position` from a given plain `string`.
* *

Loading…
Cancel
Save