/* * /// <summary> * /// utilisera * /// </summary> * /// <param name="origine"></param> * /// <param name="ligne"></param> * public static void PopulateFromBdd(GENERAL.DATAVALUES.DataValues origine, System.Data.DataRow ligne, string attributflag = "bdd") * { * try * { * foreach (GENERAL.DATAVALUES.DataValues_data item in origine.liste) * { * if (item[attributflag] != "") * { * item.value = SeyesLib3.DATA.CONNECTOR.BaseBDD.getrowobject(ligne, item[attributflag]); * } * } * } * catch (Exception ex) * { * throw new Exception("populate row " + attributflag + " Error " + ex.Message); * } * } * * */ public static ParamValuesNodeHierarchical GetHierarchicalNodes(List <ParamValuesNode> nodes, string subNodePath = null) { if (subNodePath == null) { subNodePath = "/param/"; } subNodePath = subNodePath.ToLower(); ParamValuesNodeHierarchical firstnode = new ParamValuesNodeHierarchical() { NodePath = subNodePath }; firstnode.NodeName = firstnode.NodePath.TrimEnd('/').Substring(firstnode.NodePath.TrimEnd('/').LastIndexOf('/') + 1); // Tous les noeuds inférieurs List <ParamValuesNode> subnodes = nodes.Where(n => n.Name.StartsWith(subNodePath)).ToList(); // Si c'est un noeud final qui contient la valeur firstnode.ValueNode = subnodes.FirstOrDefault(n => subNodePath.Equals(n.Name)); if (firstnode.ValueNode != null) { return(firstnode); // on authorise pas des sous noeuds si il y as une valeur } // Obtenir la premiere partie du chemin des autres subnodes Dictionary <string, List <ParamValuesNode> > othernodes = new Dictionary <string, List <ParamValuesNode> >(); foreach (ParamValuesNode subnode in subnodes) { string nextnodepath = subnode.Name.Substring(subNodePath.Length); int nextbar = nextnodepath.IndexOf('/'); if (nextbar < 1) { nextnodepath = subNodePath + nextnodepath; // on ne met pas le slash car il s'agit d'une valeur finale } else { nextnodepath = subNodePath + nextnodepath.Substring(0, nextbar) + '/'; // on remet toujours le slash } if (!othernodes.ContainsKey(nextnodepath)) { othernodes.Add(nextnodepath, new List <ParamValuesNode>()); } othernodes[nextnodepath].Add(subnode); } foreach (var item in othernodes) { var child = GetHierarchicalNodes(item.Value, item.Key); // RECURSIF if (child != null) { firstnode.ChildrenNodes.Add(child); } } return(firstnode); }
private static ParamValuesNodeHierarchical GetHierarchicalNodesSub(List <ParamValuesNode> nodes) { ParamValuesNodeHierarchical firstnode = new ParamValuesNodeHierarchical() { NodePath = "/param" }; return(firstnode); }
public string Serialize(ParamValues datavalue) { try { ParamValuesNodeHierarchical firstnode = ParamValuesTools.GetHierarchicalNodes(datavalue.GetList()); //Newtonsoft.Json.Linq.JToken jobject = Serialize_Recursif(firstnode,true); //Newtonsoft.Json.Formatting outjsonformating = Newtonsoft.Json.Formatting.None; //string jsontstr = jobject.ToString(outjsonformating); ////jsontstr = jsontstr.Replace("\r\n", "");//.Replace("\"", ""); ////jsontstr = System.Text.RegularExpressions.Regex.Unescape(jsontstr); //return jsontstr; var options = new JsonWriterOptions() { Indented = ConfigIndented }; using (var stream = new MemoryStream()) { using (var writer = new Utf8JsonWriter(stream, options)) { // on écrit pas le premier noeud param writer.WriteStartObject(); foreach (var subnode in firstnode.ChildrenNodes) { Serialize_Recursif(writer, subnode); } writer.WriteEndObject(); } string json = Encoding.UTF8.GetString(stream.ToArray()); return(json); } } catch (Exception ex) { throw new Exception("SerializeJson " + ex.Message, ex); } }
private void Serialize_Recursif(Utf8JsonWriter writer, ParamValuesNodeHierarchical node) { if (node == null) { return; } if (node.ValueNode != null) // Valeur FINALE { //Ajoute les valeurs object value = node.ValueNode.Value; string name = node.NodeName; writer.WritePropertyName(name); Nglib.DATA.KEYVALUES.KeyValuesSerializerJson.WriteValue(writer, value); } else if (node.ChildrenNodes != null && node.ChildrenNodes.Count > 0) { // Ajoute un nouvel objet writer.WritePropertyName(node.NodeName); writer.WriteStartObject(); foreach (var subnode in node.ChildrenNodes) { Serialize_Recursif(writer, subnode); } writer.WriteEndObject(); } }