public void TestFormat() { var data = new TsonObjectNode { { "A", 1 } }; string tson = Tson.Format(data, TsonFormatStyle.Compact); Assert.AreEqual("{A:1}", tson); }
private TsonObjectNode ParseObject() { TsonToken token = tokenizer.Next(); if (!token.IsLeftCurlyBrace) throw new TsonParseException(token, "Expected a '{'"); var node = new TsonObjectNode() { Token = token }; token = tokenizer.Next(); if (token.IsRightCurlyBrace) return node; while (true) { if (!token.IsString) throw new TsonParseException(token, "Expected a string"); TsonStringNode key = new TsonStringNode(token.Data as string); token = tokenizer.Next(); if (!token.IsColon) throw new TsonParseException(token, "Expected a ':'"); TsonNode value = ParseValueArrayOrObject(); node.Add(key, value); token = tokenizer.Next(); if (token.IsComma) { token = tokenizer.Next(); continue; } else if (token.IsRightCurlyBrace) break; else throw new TsonParseException(token, "Expected a '}'"); } return node; }
private TsonObjectNode ParseRootObject() { TsonToken token = tokenizer.Next(); bool hasLeftBrace = false; var node = new TsonObjectNode() { Token = token }; if (token.IsLeftCurlyBrace) { hasLeftBrace = true; token = tokenizer.Next(); if (token.IsRightCurlyBrace) return node; } else if (token.IsEnd) { return node; } while (true) { if (!token.IsString) throw new TsonParseException(token, "Expected a string"); TsonStringNode key = new TsonStringNode(token.Data as string) { Token = token }; token = tokenizer.Next(); if (!token.IsColon) throw new TsonParseException(token, "Expected a ':'"); TsonNode value = ParseValueArrayOrObject(); node.Add(key, value); token = tokenizer.Next(); if (token.IsComma) { token = tokenizer.Next(); continue; } else if (token.IsRightCurlyBrace) { if (!hasLeftBrace) throw new TsonParseException(token, "Root object did not have a '{' to match '}'"); break; } else if (token.IsEnd) { if (hasLeftBrace) throw new TsonParseException(token, "Expected '}'"); break; } else throw new TsonParseException(token, "Expected ',' or EOF" + (hasLeftBrace ? ", or '}'" : "")); } return node; }
public CompilerSetting(TsonStringNode name, TsonObjectNode parameters, List <ContentFileV4.CompilerExtension> extensions) { Name = name; Parameters = parameters; CompilerExtensions = extensions; }
private void ApplyParameters( TsonNode parentNode, string compilerName, object instance, Dictionary <string, AttributedProperty> attrProps, TsonObjectNode parameterNode) { HashSet <string> required = new HashSet <string>( from s in attrProps where s.Value.Attribute.Optional == false select s.Key); foreach (var keyValue in parameterNode.KeyValues) { var keyNode = keyValue.Key; var valueNode = keyValue.Value; AttributedProperty attrProp; attrProps.TryGetValue(keyNode.Value, out attrProp); if (attrProp == null) { WriteWarning("Supplied parameter '{0}' is not applicable to the '{1}' compiler".CultureFormat(keyNode.Value, compilerName)); continue; } PropertyInfo propertyInfo = attrProp.Property; if (!propertyInfo.CanWrite) { throw new ContentFileException(parentNode, "Unable to write to the '{0}' property of '{1}' compiler".CultureFormat(keyNode.Value, compilerName)); } object obj = null; if (propertyInfo.PropertyType == typeof(double)) { var numberNode = valueNode as TsonNumberNode; if (numberNode == null) { throw new ContentFileException(valueNode, "TSON node for '{0}' is not a number".CultureFormat(keyNode.Value)); } obj = numberNode.Value; } else if (propertyInfo.PropertyType == typeof(string)) { var numberNode = valueNode as TsonStringNode; if (numberNode == null) { throw new ContentFileException(valueNode, "TSON node for '{0}' is not a string".CultureFormat(keyNode.Value)); } obj = numberNode.Value; } else if (propertyInfo.PropertyType == typeof(bool)) { obj = ((TsonBooleanNode)valueNode).Value; } else { throw new ContentFileException( parameterNode, "Setting '{0}' parameter for compiler '{1}' must be bool, double or string".CultureFormat(keyNode.Value, compilerName)); } try { propertyInfo.SetValue(instance, obj, null); } catch (Exception e) { throw new ContentFileException(keyValue.Key, "Error setting compiler property", e); } required.Remove(keyNode.Value); } if (required.Count != 0) { throw new ContentFileException( parentNode, "Required parameter '{0}' of compiler '{1}' was not set".CultureFormat(required.First(), compilerName)); } }