private void WriteMethod(TextWriter writer, JsonMethodDescription method) { if (EcmaScriptIdentifier.IsValidIdentifier(method.Name, false)) { writer.Write(this.formatter.MethodBeginFormat, this.ProxyNamespace, method.Name, this.ConvertParamType(method.Return.Type)); } else { writer.Write(this.formatter.SafeMethodBeginFormat, this.ProxyNamespace, EcmaScriptWriter.Serialize(method.Name), this.ConvertParamType(method.Return.Type)); } foreach (JsonNamedParameterDescription param in method.Params) { this.WriteParameter(writer, param); } writer.Write(this.formatter.MethodMiddleFormat, EcmaScriptWriter.Serialize(method.Name)); if (method.Params.Length > 0) { string[] args = new string[method.Params.Length]; for (int i = 0; i < method.Params.Length; i++) { args[i] = method.Params[i].Name; } writer.Write(this.formatter.ArgsFormat, String.Join(",", args)); } else { writer.Write("null"); } writer.Write(this.formatter.MethodEndFormat); }
protected override void WriteObjectPropertyName(string name) { if (EcmaScriptIdentifier.IsValidIdentifier(name, nested: false)) { base.TextWriter.Write(name); } else { base.WriteObjectPropertyName(name); } }
private void WriteProperty(TextWriter writer, string name, object value) { if (EcmaScriptIdentifier.IsValidIdentifier(name, false)) { writer.Write(this.formatter.PropertyFormat, this.ProxyNamespace, name, EcmaScriptWriter.Serialize(value)); } else { writer.Write(this.formatter.SafePropertyFormat, this.ProxyNamespace, EcmaScriptWriter.Serialize(name), EcmaScriptWriter.Serialize(value)); } }
protected override void WriteObjectPropertyName(string name) { if (EcmaScriptIdentifier.IsValidIdentifier(name, false)) { // write out without quoting this.TextWriter.Write(name); } else { // write out as an escaped string base.WriteObjectPropertyName(name); } }
/// <summary> /// Verifies is a valid EcmaScript variable expression. /// </summary> /// <param name="varExpr">the variable expression</param> /// <returns>varExpr</returns> /// <remarks> /// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf /// /// IdentifierName = /// IdentifierStart | IdentifierName IdentifierPart /// IdentifierStart = /// Letter | '$' | '_' /// IdentifierPart = /// IdentifierStart | Digit /// </remarks> public static bool IsValidIdentifier(string varExpr, bool nested) { if (String.IsNullOrEmpty(varExpr)) { return(false); } if (nested) { string[] parts = varExpr.Split('.'); foreach (string part in parts) { if (!EcmaScriptIdentifier.IsValidIdentifier(part, false)) { return(false); } } return(true); } if (EcmaScriptIdentifier.IsReservedWord(varExpr)) { return(false); } bool indentPart = false; foreach (char ch in varExpr) { if (indentPart && Char.IsDigit(ch)) { // digits are only allowed after first char continue; } // can be start or part if (Char.IsLetter(ch) || ch == '_' || ch == '$') { indentPart = true; continue; } return(false); } return(true); }
/// <summary> /// Ensures is a valid EcmaScript variable expression. /// </summary> /// <param name="varExpr">the variable expression</param> /// <returns>varExpr</returns> public static string EnsureValidIdentifier(string varExpr, bool nested, bool throwOnEmpty) { if (String.IsNullOrEmpty(varExpr)) { if (throwOnEmpty) { throw new ArgumentException("Variable expression is empty."); } return(String.Empty); } varExpr = varExpr.Replace(" ", ""); if (!EcmaScriptIdentifier.IsValidIdentifier(varExpr, nested)) { throw new ArgumentException("Variable expression \"" + varExpr + "\" is not supported."); } return(varExpr); }