private static void WriteCollection(BinaryWriter w, PmlCollection value) { WriteUInt32(w, (UInt32)value.Count); foreach (PmlElement o in value) { WriteElementTo(o, w); } }
public static PmlElement ReadElement(System.Xml.XmlNode X) { PmlType pType; bool pTypeFound = false; pType = PmlType.Null; pTypeFound = true; if (X.Attributes != null && X.Attributes.Count > 0 && X.Attributes["type"] != null) { switch (X.Attributes["type"].Value.ToLowerInvariant()) { case "binary": pType = PmlType.Binary; break; case "collection": pType = PmlType.Collection; break; case "dictionary": pType = PmlType.Dictionary; break; case "string": pType = PmlType.String; break; case "null": pType = PmlType.Null; break; case "integer": pType = PmlType.Integer; break; default: pTypeFound = false; break; } } else { pTypeFound = false; } if (!pTypeFound) { if (X.HasChildNodes) { if (X.ChildNodes.Count == 1 && X.FirstChild.NodeType == System.Xml.XmlNodeType.Text) { Int64 dummy; UInt64 dummyu; if (Int64.TryParse(X.FirstChild.Value, out dummy) || UInt64.TryParse(X.FirstChild.Value, out dummyu)) { pType = PmlType.Integer; } else { pType = PmlType.String; } } else if (X.FirstChild.Name == "item") { pType = PmlType.Collection; } else { pType = PmlType.Dictionary; } } else { pType = PmlType.Null; } } switch (pType) { case PmlType.Null: return(new PmlNull()); case PmlType.Binary: if (X.FirstChild == null) { return(new PmlBinary(new byte[0])); } else { return(new PmlBinary(Convert.FromBase64String(X.FirstChild.Value))); } case PmlType.Integer: return(new PmlInteger(X.FirstChild.Value)); case PmlType.String: if (X.FirstChild == null) { return(new PmlString("")); } else { return(new PmlString(X.FirstChild.Value)); } case PmlType.Collection: PmlCollection C = new PmlCollection(); foreach (XmlNode N in X.ChildNodes) { C.Add(ReadElement(N)); } return(C); case PmlType.Dictionary: PmlDictionary D = new PmlDictionary(); foreach (XmlNode N in X.ChildNodes) { D.Add(N.Name, ReadElement(N)); } return(D); default: return(null); } }
private static PmlElement ReadElementFrom(TextReader reader) { int c = FindNextToken(reader); if (c == -1) { return(null); } if (c == '{') { reader.Read(); c = FindNextToken(reader); PmlDictionary d = new PmlDictionary(); while (c != '}') { if (c == -1) { throw new EndOfStreamException(); } String k = ReadString(reader); c = FindNextToken(reader); if (c != ':') { throw new JsonFormatException("Expected colon, got " + (Char)c); } reader.Read(); PmlElement e = ReadElementFrom(reader); d.Add(k, e); c = FindNextToken(reader); if (c == '}') { break; } if (c != ',') { throw new JsonFormatException("Expected comma or closing curly brace, got " + (Char)c); } c = reader.Read(); c = FindNextToken(reader); } reader.Read(); return(d); } else if (c == '[') { reader.Read(); c = FindNextToken(reader); PmlCollection l = new PmlCollection(); while (c != ']') { if (c == -1) { throw new EndOfStreamException(); } PmlElement e = ReadElementFrom(reader); l.Add(e); c = FindNextToken(reader); if (c == ']') { break; } if (c != ',') { throw new JsonFormatException("Expected comma or closing curly brace, got " + (Char)c); } c = reader.Read(); } reader.Read(); return(l); } else if (c == '"' || c == '\'') { return(ReadString(reader)); } else if (c == 'n') { ExpectLiteral(reader, "null"); return(new PmlNull()); } else if (c == 't') { ExpectLiteral(reader, "true"); return(1); } else if (c == 'f') { ExpectLiteral(reader, "false"); return(0); } else if (c == '-' || (c >= '0' && c <= '9')) { StringBuilder sb = new StringBuilder(); while (c == '.' || c == '-' || c == '+' || c == '.' || c == 'e' || c == 'E' || (c >= '0' && c <= '9')) { sb.Append((Char)c); reader.Read(); c = reader.Peek(); } String str = sb.ToString(); if (str.IndexOf(".", StringComparison.OrdinalIgnoreCase) != -1 || str.IndexOf("e", StringComparison.OrdinalIgnoreCase) != -1) { Double number = Double.Parse(str, NumberStyles.Any, CultureInfo.InvariantCulture); return(str); } else { return(Int64.Parse(str, NumberStyles.Any, CultureInfo.InvariantCulture)); } } else { throw new JsonFormatException("Expected JSON token, got " + (Char)c); } }
private static PmlElement ReadData(BinaryReader Reader, AmfDataType EType) { switch (EType) { case AmfDataType.Number: Double d = ReadDouble(Reader); if (d == (double)(Int64)d) { return(new PmlInteger((Int64)d)); } else if (d == (double)(UInt64)d) { return(new PmlInteger((UInt64)d)); } else { return(new PmlString(d.ToString())); } case AmfDataType.Boolean: return(new PmlInteger(Reader.ReadByte())); case AmfDataType.String: return(new PmlString(ReadShortString(Reader))); case AmfDataType.Array: PmlCollection ElementC = new PmlCollection(); int size = ReadInt32(Reader); for (int i = 0; i < size; ++i) { ElementC.Add(ReadElementFrom(Reader)); } return(ElementC); case AmfDataType.Date: return(new PmlString(ReadDate(Reader).ToString())); case AmfDataType.LongString: return(new PmlString(ReadLongString(Reader))); case AmfDataType.TypedObject: ReadShortString(Reader); return(ReadUntypedObject(Reader)); case AmfDataType.MixedArray: return(ReadDictionary(Reader)); case AmfDataType.Null: case AmfDataType.Undefined: case AmfDataType.End: return(new PmlNull()); case AmfDataType.UntypedObject: return(ReadUntypedObject(Reader)); case AmfDataType.Xml: return(new PmlString(ReadLongString(Reader))); case AmfDataType.MovieClip: case AmfDataType.ReferencedObject: case AmfDataType.TypeAsObject: case AmfDataType.Recordset: default: throw new NotSupportedException("The AMF type is not supported: " + EType.ToString()); } }
private static PmlElement ReadElementFrom(BinaryReader Reader) { Byte EType = Reader.ReadByte(); switch (EType) { case 0: return(new PmlNull()); case 1: PmlDictionary ElementD = new PmlDictionary(); do { byte B = Reader.ReadByte(); if (B == 0) { return(ElementD); } else if (B == 1) { ElementD.Add(Reader.ReadString(), ReadElementFrom(Reader)); } else { return(null); } }while (true); case 2: PmlCollection ElementC = new PmlCollection(); do { byte B = Reader.ReadByte(); if (B == 0) { return(ElementC); } else if (B == 1) { ElementC.Add(ReadElementFrom(Reader)); } else { return(null); } }while (true); case 10: int Len = 0; Len = Reader.ReadInt32(); return(new PmlBinary(Reader.ReadBytes(Len))); case 11: return(new PmlString(Reader.ReadString())); case 20: { byte B = Reader.ReadByte(); if (B == 0) { return(new PmlInteger(Reader.ReadUInt64())); } else if (B == 1) { return(new PmlInteger(Reader.ReadInt64())); } else { return(null); } } case 21: return(Reader.ReadBoolean()); case 22: return(Reader.ReadDouble()); default: throw new Exception("Unknown PML type code " + EType.ToString()); } }