static private void XmlToJsonLayer(XmlNode layerNode, JsonNode jsonNode, XmlToJsonConverter converter) { XmlNode nameXmlNode = layerNode.SelectSingleNode("Name"); XmlNode titleXmlNode = layerNode.SelectSingleNode("Title"); string name = nameXmlNode.InnerText; string title = ""; if (titleXmlNode != null) { title = titleXmlNode.InnerText; } jsonNode.Add("name", new JsonValue(name)); jsonNode.Add("title", new JsonValue(title)); XmlNodeList sublayersXmlNodes = layerNode.SelectNodes("Layer"); if (sublayersXmlNodes.Count > 0) { XmlToJsonSublayers(sublayersXmlNodes, jsonNode, converter); } XmlNode attributesXmlNode = layerNode.SelectSingleNode("Attributes"); if (attributesXmlNode != null) { JsonList layerAttributes = new JsonList(); XmlToJsonAttributes(attributesXmlNode, layerAttributes, converter); jsonNode.Add("attributes", layerAttributes); } }
public void should_add_named_fields_to_arrays() { var node = new JsonNode(NodeType.Array, new Options()); node.NodeType.ShouldEqual(NodeType.Array); node.Add("item1", NodeType.Value, Metadata.Empty, x => x.Value = "oh"); node.Add("item2", NodeType.Value, Metadata.Empty, x => x.Value = "hai"); node.Encode().ShouldEqual("[\"oh\",\"hai\"]"); }
public void should_add_named_fields_to_objects() { var node = new JsonNode(NodeType.Object, new Options()); node.NodeType.ShouldEqual(NodeType.Object); node.Add("field1", NodeType.Value, Metadata.Empty, x => x.Value = "oh"); node.Add("field2", NodeType.Value, Metadata.Empty, x => x.Value = "hai"); node.Encode().ShouldEqual("{\"field1\":\"oh\",\"field2\":\"hai\"}"); }
static private void XmlToJsonAttribute(XmlNode attributeNode, JsonNode jsonNode, XmlToJsonConverter converter) { XmlAttributeCollection xmlAttributes = attributeNode.Attributes; string name = xmlAttributes.GetNamedItem("name").InnerText; string type = xmlAttributes.GetNamedItem("type").InnerText; jsonNode.Add("name", new JsonValue(name)); jsonNode.Add("type", new JsonValue(type)); }
public void should_set_field_string_value() { var node = new JsonNode(NodeType.Object, new Options()) { NodeType = NodeType.Object }; node.Add("field1", NodeType.Value, Metadata.Empty, x => x.Value = "hai"); node.Add("field2", NodeType.Value, Metadata.Empty, x => x.Value = 'y'); node.Encode().ShouldEqual("{\"field1\":\"hai\",\"field2\":\"y\"}"); }
public void should_set_array_string_value() { var node = new JsonNode(NodeType.Array, new Options()) { NodeType = NodeType.Array }; node.Add(NodeType.Value, Metadata.Empty, x => x.Value = "hai"); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = 'y'); node.Encode().ShouldEqual("[\"hai\",\"y\"]"); }
private void Set(JsonNode aVal) { if (m_Key == null) { m_Node.Add(aVal); } else { m_Node.Add(m_Key, aVal); } m_Node = null; // Be GC friendly. }
/// <summary> /// Decode the message. /// </summary> /// <returns> /// The message. /// </returns> /// <param name='msg'> /// JsonObject. /// </param> /// <param name='proto'> /// JsonObject. /// </param> /// <param name='length'> /// int. /// </param> private JsonNode decodeMsg(JsonNode msg, JsonNode proto, int length) { while (this.offset < length) { Dictionary <string, int> head = this.getHead(); int tag; if (head.TryGetValue("tag", out tag)) { JsonNode _tags = null; if (proto.TryGetValue("__tags", out _tags)) { JsonNode name = null; if (_tags.TryGetValue(tag.ToString(), out name)) { JsonNode value = null; if (proto.TryGetValue(name.AsString, out value)) { JsonNode option; if (value.TryGetValue("option", out option)) { switch (option.ToString()) { case "optional": case "required": JsonNode type = null; if (value.TryGetValue("type", out type)) { msg.Add(name.AsString, this.decodeProp(type.AsString, proto)); } break; case "repeated": JsonNode _name = null; if (!msg.TryGetValue(name.AsString, out _name)) { msg.Add(name.AsString, new JsonArray()); } JsonNode value_type = null; if (msg.TryGetValue(name.AsString, out _name) && (value.TryGetValue("type", out value_type))) { decodeArray(_name as JsonArray, value_type.AsString, proto); } break; } } } } } } } return(msg); }
public void should_fail_to_add_unamed_fields_to_objects() { var node = new JsonNode(NodeType.Object, new Options()); node.NodeType.ShouldEqual(NodeType.Object); Assert.Throws <UnnamedChildrenNotSupportedException>(() => node.Add(NodeType.Value, Metadata.Empty, x => x.Value = "hai")); }
static private void XmlToJsonAttributes(XmlNode attributesNode, JsonNode jsonNode, XmlToJsonConverter converter) { foreach (XmlNode attributeXmlNode in attributesNode.ChildNodes) { JsonObject layerAttribute = new JsonObject(); converter.Convert(attributeXmlNode, layerAttribute); jsonNode.Add(layerAttribute); } }
public void should_set_field_null_value() { var node = new JsonNode(NodeType.Object, new Options()) { NodeType = NodeType.Object }; node.Add("field1", NodeType.Value, Metadata.Empty, x => x.Value = null); node.Encode().ShouldEqual("{\"field1\":null}"); }
public void should_set_array_object_value() { var node = new JsonNode(NodeType.Array, new Options()) { NodeType = NodeType.Array }; node.Add(NodeType.Object, Metadata.Empty, x => {}); node.Encode().ShouldEqual("[{}]"); }
public void should_set_field_object_value() { var node = new JsonNode(NodeType.Object, new Options()) { NodeType = NodeType.Object }; node.Add("field1", NodeType.Object, Metadata.Empty, x => {}); node.Encode().ShouldEqual("{\"field1\":{}}"); }
internal override async Task <JsonNode> ToJson() { JsonNode node = await base.ToJson(); if (this.Manifest != null) { JsonNode infrastructureDependencies = this.Manifest["infrastructure_dependencies"]; JsonNode serviceDependencies = this.Manifest["service_dependencies"]; if (infrastructureDependencies != null) { node.Add("infrastructure_dependencies", infrastructureDependencies); } if (serviceDependencies != null) { node.Add("service_dependencies", serviceDependencies); } } return(node); }
static private void XmlToJsonSublayers(XmlNodeList sublayersXmlNodes, JsonNode jsonNode, XmlToJsonConverter converter) { JsonList sublayersJsonNodes = new JsonList(); foreach (XmlNode sublayerXmlNode in sublayersXmlNodes) { JsonObject sublayerJsonNode = new JsonObject(); converter.Convert(sublayerXmlNode, sublayerJsonNode); sublayersJsonNodes.Add(sublayerJsonNode); } jsonNode.Add("sublayers", sublayersJsonNodes); }
internal override async Task <JsonNode> ToJson() { JsonNode node = await base.ToJson().ConfigureAwait(false); if (this.ShowConnectionStringInJson) { string connectionString = string.Empty; if (this.ConnectionStringProvider?.TryGetConnectionString(out connectionString) == true) { node.Add("value", connectionString); } } return(node); }
public static JsonNode Parse(string aJSON) { Stack <JsonNode> stack = new Stack <JsonNode> (); JsonNode ctx = null; int i = 0; string Token = ""; string TokenName = ""; bool QuoteMode = false; while (i < aJSON.Length) { switch (aJSON [i]) { case '{': if (QuoteMode) { Token += aJSON [i]; break; } stack.Push(new JsonClass()); if (ctx != null) { TokenName = TokenName.Trim(); if (ctx is JsonArray) { ctx.Add(stack.Peek()); } else if (TokenName != "") { ctx.Add(TokenName, stack.Peek()); } } TokenName = ""; Token = ""; ctx = stack.Peek(); break; case '[': if (QuoteMode) { Token += aJSON [i]; break; } stack.Push(new JsonArray()); if (ctx != null) { TokenName = TokenName.Trim(); if (ctx is JsonArray) { ctx.Add(stack.Peek()); } else if (TokenName != "") { ctx.Add(TokenName, stack.Peek()); } } TokenName = ""; Token = ""; ctx = stack.Peek(); break; case '}': case ']': if (QuoteMode) { Token += aJSON [i]; break; } if (stack.Count == 0) { throw new Exception("JSON Parse: Too many closing brackets"); } stack.Pop(); if (Token != "") { TokenName = TokenName.Trim(); if (ctx is JsonArray) { ctx.Add(Token); } else if (TokenName != "") { ctx.Add(TokenName, Token); } } TokenName = ""; Token = ""; if (stack.Count > 0) { ctx = stack.Peek(); } break; case ':': if (QuoteMode) { Token += aJSON [i]; break; } TokenName = Token; Token = ""; break; case '"': QuoteMode ^= true; break; case ',': if (QuoteMode) { Token += aJSON [i]; break; } if (Token != "") { if (ctx is JsonArray) { ctx.Add(Token); } else if (TokenName != "") { ctx.Add(TokenName, Token); } } TokenName = ""; Token = ""; break; case '\r': case '\n': break; case ' ': case '\t': if (QuoteMode) { Token += aJSON [i]; } break; case '\\': ++i; if (QuoteMode) { char C = aJSON [i]; switch (C) { case 't': Token += '\t'; break; case 'r': Token += '\r'; break; case 'n': Token += '\n'; break; case 'b': Token += '\b'; break; case 'f': Token += '\f'; break; case 'u': { string s = aJSON.Substring(i + 1, 4); Token += (char)int.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier); i += 4; break; } default: Token += C; break; } } break; default: Token += aJSON [i]; break; } ++i; } if (QuoteMode) { throw new Exception("JSON Parse: Quotation marks seems to be messed up."); } return(ctx); }
public void should_set_array_number_value() { var node = new JsonNode(NodeType.Array, new Options()) { NodeType = NodeType.Array }; node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (decimal)1.1); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (float)2.2); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (double)3.3); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (sbyte)4); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (byte)5); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (short)6); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (ushort)7); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (int)8); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (uint)9); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (long)10); node.Add(NodeType.Value, Metadata.Empty, x => x.Value = (ulong)11); node.Encode().ShouldEqual("[1.1,2.2,3.3,4,5,6,7,8,9,10,11]"); }
public void Save() { JsonNode root = new JsonNode(); JsonNode collections = new JsonNode(); foreach (KeyValuePair<string, List<List<string>>> keyValuePair in m_collections) { JsonArray jsonArray = new JsonArray(); foreach (string item in keyValuePair.Value.SelectMany(n => n)) jsonArray.Add(item); collections.Add(new JsonElement(keyValuePair.Key, jsonArray)); } root.Add(new JsonElement("collections", collections)); JsonNode filetimeStamps = new JsonNode(); foreach (KeyValuePair<string, DateTime> keyValuePair in m_fileTimeStamps) { filetimeStamps.Add(new JsonElement(keyValuePair.Key.Replace("\\", "\\\\"), keyValuePair.Value)); } root.Add(new JsonElement("fileTimeStamps", filetimeStamps)); StreamWriter streamWriter = File.CreateText(m_project.Children[0].Path + "\\autocompletdata.json"); streamWriter.Write(root.ToString()); streamWriter.Close(); }
public void SaveSetup() { JsonNode root = new JsonNode {{"layouttype", m_selectedLayoutType}}; JsonArray layouts = new JsonArray(); foreach (LayoutElementViewModel editorLayoutViewModel in m_layoutElements) { JsonNode layout = new JsonNode(); JsonArray openFiles = new JsonArray(); foreach (IFileViewModel fileViewModel in editorLayoutViewModel.OpenFiles.Where(n => n.Path != null)) openFiles.Add(fileViewModel.Path); layout.Add(new JsonElement("openfiles", openFiles)); if (editorLayoutViewModel.SelectedFile != null && editorLayoutViewModel.SelectedFile.Path != null) layout.Add(new JsonElement("selectedFile", editorLayoutViewModel.SelectedFile.Path)); layout.Add("isactive", editorLayoutViewModel == m_activeLayoutElement); layouts.Add(layout); } root.Add("layouts", layouts); StreamWriter streamWriter = File.CreateText(Properties.Settings.Default.SettingPath + "\\openFiles.json"); streamWriter.Write(root.ToString()); streamWriter.Close(); }
static void Package() { #region Check if (EditorApplication.isCompiling) { EditorUtility.DisplayDialog("警告", "资源正常编译,请完成后重试!", "OK"); return; } BundleConfig config = AssetDatabase.LoadAssetAtPath <BundleConfig>("Assets/assetbundle.asset"); if (config == null) { EditorUtility.DisplayDialog("警告", "未检测到对应的构建规则,请创建后重试!", "OK"); return; } if (!Directory.Exists(bundleSavePath)) { Directory.CreateDirectory(bundleSavePath); } #endregion Dictionary <string, BundleInfo> bundleInfos = new Dictionary <string, BundleInfo>(); foreach (var filter in config.filters) { #region 校验空值 if (!Directory.Exists(filter.assetPath)) { Debug.LogWarning("Cannot Find Path : " + filter.assetPath); continue; } #endregion #region 文件夹打包 if (filter.isFolderPack) { if (filter.isChildFolder) { string[] folders = Directory.GetDirectories(filter.assetPath); foreach (var folder in folders) { string folderPath = folder.Replace("\\", "/"); BundleInfo info = AddToSet(bundleInfos, folderPath, false); string[] folderfiles = Directory.GetFiles(folderPath); foreach (var folderfile in folderfiles) { string path = folderfile.Replace("\\", "/"); if (folderfile.ToLower().EndsWith(".meta")) { continue; } if (folderfile.ToLower().EndsWith(".cs")) { continue; } if (folderfile.ToLower().EndsWith(".shader")) { AddToShaderSet(bundleInfos, path); continue; } info.AddAsset(path); } } } else { BundleInfo info = AddToSet(bundleInfos, filter.assetPath, false); string[] folderfiles = Directory.GetFiles(filter.assetPath); foreach (var folderfile in folderfiles) { string path = folderfile.Replace("\\", "/"); if (folderfile.ToLower().EndsWith(".meta")) { continue; } if (folderfile.ToLower().EndsWith(".cs")) { continue; } if (folderfile.ToLower().EndsWith(".shader")) { AddToShaderSet(bundleInfos, path); continue; } info.AddAsset(path); } } continue; } #endregion #region 后缀打包 #region 处理后缀 if (string.IsNullOrEmpty(filter.extension)) { Debug.LogWarning("Cannot Match Extension : " + filter.assetPath); continue; } string[] exs = filter.extension.Split('|'); List <string> extends = new List <string>(); for (int i = 0; i < exs.Length; i++) { string ex = exs[i].Trim().ToLower(); if (!extends.Contains(ex)) { extends.Add(ex); } } #endregion #region 判断整体 string wholeBundleName = filter.assetPath.Replace("\\", "/"); BundleInfo whole; if (!bundleInfos.TryGetValue(wholeBundleName, out whole)) { if (!filter.isSingle) { whole = new BundleInfo(wholeBundleName, false); } } #endregion #region 单个及依赖 string[] files = Directory.GetFiles(filter.assetPath); foreach (var file in files) { if (!extends.Contains(Path.GetExtension(file).ToLower())) { continue; } string path = file.Replace("\\", "/"); if (path.ToLower().EndsWith(".shader"))//所有Shader单独处理,即使填写规则也全部放到一起 { AddToShaderSet(bundleInfos, path); continue; } BundleInfo single = null; if (!filter.isSingle) //整体打包 { whole.AddAsset(path); } else { single = AddToSet(bundleInfos, path); single.AddAsset(path); } if (filter.isDepend)//依赖打包 { string[] deps = AssetDatabase.GetDependencies(path); foreach (var dep in deps) { if (dep.Equals(path)) { continue; } if (dep.EndsWith(".cs")) { continue; } var depPath = dep.Replace("\\", "/"); if (dep.ToLower().EndsWith(".shader")) { AddToShaderSet(bundleInfos, depPath); AddDependency(whole, single, filter.isSingle, shaderSet); continue; } if (filter.isDependSplit) { BundleInfo depSingle = AddToSet(bundleInfos, depPath); depSingle.AddAsset(depPath); AddDependency(whole, single, filter.isSingle, depPath); } else { string wholeName = string.IsNullOrEmpty(filter.dependName.Trim()) ? "CommonDependencies" : filter.dependName; BundleInfo info; if (!bundleInfos.TryGetValue(wholeName, out info)) { info = new BundleInfo(wholeName, false); bundleInfos.Add(wholeName, info); } info.AddAsset(depPath); AddDependency(whole, single, filter.isSingle, wholeName); } } } } #endregion #endregion } #region 除旧资源 if (File.Exists(manifestPath)) { JsonNode oldRoot = new JsonParser().Load(File.ReadAllBytes(manifestPath)); Dictionary <string, JsonNode> oldAssets = (Dictionary <string, JsonNode>)oldRoot["BundleManifest"]; List <string> willDeletes = new List <string>(); foreach (var oldAsset in oldAssets) { if (!bundleInfos.ContainsKey(oldAsset.Key)) { willDeletes.Add(oldAsset.Key); } } foreach (var willDelete in willDeletes) { Debug.Log("DELETE BUNDLE:: " + willDelete); string bundleName = (string)oldAssets[willDelete]["BundleName"]; string bundlePath = bundleSavePath + "/" + bundleName; if (File.Exists(bundlePath)) { File.Delete(bundlePath); } if (File.Exists(bundlePath + ".manifest")) { File.Delete(bundlePath + ".manifest"); } } } #endregion #region 打包 List <AssetBundleBuild> builds = new List <AssetBundleBuild>(); foreach (var bundleInfo in bundleInfos) { AssetBundleBuild build = new AssetBundleBuild(); build.assetBundleName = bundleInfo.Value.bundleName; build.assetNames = bundleInfo.Value.assets.ToArray(); builds.Add(build); } BuildPipeline.BuildAssetBundles(bundleSavePath, builds.ToArray(), BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows); #endregion #region 新规则构建 JsonNode root = JsonNode.NewTable(); JsonNode assets = JsonNode.NewTable(); root["Version"] = JsonNode.NewString("1.0"); root["BundleCount"] = JsonNode.NewInt(bundleInfos.Count); root["BundleManifest"] = assets; foreach (var bundleInfo in bundleInfos.Values) { JsonNode node = JsonNode.NewTable(); //node["AssetPath"] = JsonNode.NewString(bundleInfo.assetPath); node["BundleName"] = JsonNode.NewString(bundleInfo.bundleName); //node["Single"] = JsonNode.NewBool(bundleInfo.isSingle); node["AssetCount"] = JsonNode.NewInt(bundleInfo.assets.Count); long length; node["Hash"] = JsonNode.NewString(bundleInfo.GetHash(bundleSavePath, out length)); node["Length"] = JsonNode.NewNumber(length); JsonNode dependencies = JsonNode.NewArray(); foreach (var dep in bundleInfo.dependencies) { dependencies.Add(JsonNode.NewString(dep)); } node["Dependencies"] = dependencies; assets[bundleInfo.assetPath] = node; } if (File.Exists(manifestPath)) { File.Delete(manifestPath); } File.WriteAllText(manifestPath, new JsonPrinter(true).String(root), new UTF8Encoding(false)); AssetDatabase.Refresh(); #endregion }
private JsonNode ParseDictionary() { if (lexer.GetCurrentTokenType() != TokenType.DictionaryStart) throw CreateExpectedException(lexer.GetCurrentToken(), TokenType.DictionaryStart); JsonNode result = new JsonNode(NodeType.Dictionary); lexer.NextToken(); while (true) { if (lexer.GetCurrentTokenType() == TokenType.DictionaryEnd) { lexer.NextToken(); break; } if (lexer.GetCurrentTokenType() != TokenType.String) throw CreateExpectedException(lexer.GetCurrentToken(), TokenType.String); string key = lexer.GetCurrentToken().Value; lexer.NextToken(); if (lexer.GetCurrentTokenType() != TokenType.Colon) throw CreateExpectedException(lexer.GetCurrentToken(), TokenType.Colon); lexer.NextToken(); JsonNode child = Parse(); result.Add(key, child); if (lexer.GetCurrentTokenType() != TokenType.Comma) { if (lexer.GetCurrentTokenType() == TokenType.DictionaryEnd) { lexer.NextToken(); break; } throw CreateExpectedException(lexer.GetCurrentToken(), new TokenType[] { TokenType.DictionaryEnd, TokenType.Comma }); } lexer.NextToken(); } return result; }
private JsonNode ParseList() { if (lexer.GetCurrentTokenType() != TokenType.ArrayStart) throw CreateExpectedException(lexer.GetCurrentToken(), TokenType.ArrayStart); JsonNode result = new JsonNode(NodeType.List); lexer.NextToken(); while (true) { if (lexer.GetCurrentTokenType() == TokenType.ArrayEnd) { lexer.NextToken(); break; } JsonNode child = Parse(); result.Add(child); if (lexer.GetCurrentTokenType() != TokenType.Comma) { if (lexer.GetCurrentTokenType() == TokenType.ArrayEnd) { lexer.NextToken(); break; } throw CreateExpectedException(lexer.GetCurrentToken(), new TokenType[] { TokenType.ArrayEnd, TokenType.Comma }); } lexer.NextToken(); } return result; }
public void should_set_field_number_value() { var node = new JsonNode(NodeType.Object, new Options()) { NodeType = NodeType.Object }; node.Add("field1", NodeType.Value, Metadata.Empty, x => x.Value = (decimal)1.1); node.Add("field2", NodeType.Value, Metadata.Empty, x => x.Value = (float)2.2); node.Add("field3", NodeType.Value, Metadata.Empty, x => x.Value = (double)3.3); node.Add("field4", NodeType.Value, Metadata.Empty, x => x.Value = (sbyte)4); node.Add("field5", NodeType.Value, Metadata.Empty, x => x.Value = (byte)5); node.Add("field6", NodeType.Value, Metadata.Empty, x => x.Value = (short)6); node.Add("field7", NodeType.Value, Metadata.Empty, x => x.Value = (ushort)7); node.Add("field8", NodeType.Value, Metadata.Empty, x => x.Value = (int)8); node.Add("field9", NodeType.Value, Metadata.Empty, x => x.Value = (uint)9); node.Add("field10", NodeType.Value, Metadata.Empty, x => x.Value = (long)10); node.Add("field11", NodeType.Value, Metadata.Empty, x => x.Value = (ulong)11); node.Encode().ShouldEqual("{" + "\"field1\":1.1," + "\"field2\":2.2," + "\"field3\":3.3," + "\"field4\":4," + "\"field5\":5," + "\"field6\":6," + "\"field7\":7," + "\"field8\":8," + "\"field9\":9," + "\"field10\":10," + "\"field11\":11" + "}"); }