public void DeserializeReal() { String realOne = "r1123412345.465711"; OSD llsdOne = OSDParser.DeserializeLLSDNotation(realOne); Assert.AreEqual(OSDType.Real, llsdOne.Type); Assert.AreEqual(1123412345.465711d, llsdOne.AsReal()); String realTwo = "r-11234684.923411"; OSD llsdTwo = OSDParser.DeserializeLLSDNotation(realTwo); Assert.AreEqual(OSDType.Real, llsdTwo.Type); Assert.AreEqual(-11234684.923411d, llsdTwo.AsReal()); String realThree = "r1"; OSD llsdThree = OSDParser.DeserializeLLSDNotation(realThree); Assert.AreEqual(OSDType.Real, llsdThree.Type); Assert.AreEqual(1d, llsdThree.AsReal()); String realFour = "r2.0193899999999998204e-06"; OSD llsdFour = OSDParser.DeserializeLLSDNotation(realFour); Assert.AreEqual(OSDType.Real, llsdFour.Type); Assert.AreEqual(2.0193899999999998204e-06d, llsdFour.AsReal()); String realFive = "r0"; OSD llsdFive = OSDParser.DeserializeLLSDNotation(realFive); Assert.AreEqual(OSDType.Real, llsdFive.Type); Assert.AreEqual(0d, llsdFive.AsReal()); }
public void DeserializeReal() { OSD llsdReal = OSDParser.DeserializeLLSDBinary(binaryReal); Assert.AreEqual(OSDType.Real, llsdReal.Type); Assert.AreEqual(947835.234d, llsdReal.AsReal()); }
public static object SerializeLisp(OSD osd) { switch (osd.Type) { case OSDType.Unknown: throw new InvalidCastException(); case OSDType.Boolean: return(osd.AsBoolean()); case OSDType.Integer: return(osd.AsInteger()); case OSDType.Real: return(osd.AsReal()); case OSDType.String: return(osd.AsString()); case OSDType.Date: return(osd.AsDate()); case OSDType.URI: return(osd.AsUri()); case OSDType.UUID: return(osd.AsUUID()); case OSDType.Binary: return(osd.AsBinary()); case OSDType.Array: OSDArray args = (OSDArray)osd; Cons ret = null; for (int i = args.Count - 1; i >= 0; --i) { ret = new Cons(args[i], ret); } return(ret); case OSDType.Map: Cons list = null; OSDMap map = (OSDMap)osd; foreach (KeyValuePair <string, OSD> kvp in map) { Cons kv = new Cons(kvp.Key, new Cons(SerializeLisp(kvp.Value))); list = new Cons(kv, list); } return(Cons.Reverse(list)); default: return(osd); } }
public static JsonData SerializeJson(OSD osd) { switch (osd.Type) { case OSDType.Boolean: return new JsonData(osd.AsBoolean()); case OSDType.Integer: return new JsonData(osd.AsInteger()); case OSDType.Real: return new JsonData(osd.AsReal()); case OSDType.String: case OSDType.Date: case OSDType.URI: case OSDType.UUID: return new JsonData(osd.AsString()); case OSDType.Binary: byte[] binary = osd.AsBinary(); JsonData jsonbinarray = new JsonData(); jsonbinarray.SetJsonType(JsonType.Array); for (int i = 0; i < binary.Length; i++) jsonbinarray.Add(new JsonData(binary[i])); return jsonbinarray; case OSDType.Array: JsonData jsonarray = new JsonData(); jsonarray.SetJsonType(JsonType.Array); OSDArray array = (OSDArray)osd; for (int i = 0; i < array.Count; i++) jsonarray.Add(SerializeJson(array[i])); return jsonarray; case OSDType.Map: JsonData jsonmap = new JsonData(); jsonmap.SetJsonType(JsonType.Object); OSDMap map = (OSDMap)osd; foreach (KeyValuePair<string, OSD> kvp in map) { // Default values will not be serialized to the jsonmap JsonData data = SerializeJsonNoDefaults(kvp.Value); if (data != null) jsonmap[kvp.Key] = data; } return jsonmap; case OSDType.Unknown: default: return new JsonData(); } }
public void SerializeReal() { OSD llsdOne = OSD.FromReal(12987234.723847d); string sOne = OSDParser.SerializeLLSDNotation(llsdOne); OSD llsdOneDS = OSDParser.DeserializeLLSDNotation(sOne); Assert.AreEqual(OSDType.Real, llsdOneDS.Type); Assert.AreEqual(12987234.723847d, llsdOneDS.AsReal()); OSD llsdTwo = OSD.FromReal(-32347892.234234d); string sTwo = OSDParser.SerializeLLSDNotation(llsdTwo); OSD llsdTwoDS = OSDParser.DeserializeLLSDNotation(sTwo); Assert.AreEqual(OSDType.Real, llsdTwoDS.Type); Assert.AreEqual(-32347892.234234d, llsdTwoDS.AsReal()); OSD llsdThree = OSD.FromReal(Double.MaxValue); string sThree = OSDParser.SerializeLLSDNotation(llsdThree); OSD llsdThreeDS = OSDParser.DeserializeLLSDNotation(sThree); Assert.AreEqual(OSDType.Real, llsdThreeDS.Type); Assert.AreEqual(Double.MaxValue, llsdThreeDS.AsReal()); OSD llsdFour = OSD.FromReal(Double.MinValue); string sFour = OSDParser.SerializeLLSDNotation(llsdFour); OSD llsdFourDS = OSDParser.DeserializeLLSDNotation(sFour); Assert.AreEqual(OSDType.Real, llsdFourDS.Type); Assert.AreEqual(Double.MinValue, llsdFourDS.AsReal()); OSD llsdFive = OSD.FromReal(-1.1123123E+50d); string sFive = OSDParser.SerializeLLSDNotation(llsdFive); OSD llsdFiveDS = OSDParser.DeserializeLLSDNotation(sFive); Assert.AreEqual(OSDType.Real, llsdFiveDS.Type); Assert.AreEqual(-1.1123123E+50d, llsdFiveDS.AsReal()); OSD llsdSix = OSD.FromReal(2.0193899999999998204e-06); string sSix = OSDParser.SerializeLLSDNotation(llsdSix); OSD llsdSixDS = OSDParser.DeserializeLLSDNotation(sSix); Assert.AreEqual(OSDType.Real, llsdSixDS.Type); Assert.AreEqual(2.0193899999999998204e-06, llsdSixDS.AsReal()); }
object ParseJsonNode(OSD node) { if (node.Type == OSDType.Integer) { return(new LSL_Integer(node.AsInteger())); } if (node.Type == OSDType.Boolean) { return(new LSL_Integer(node.AsBoolean() ? 1 : 0)); } if (node.Type == OSDType.Real) { return(new LSL_Float(node.AsReal())); } if (node.Type == OSDType.UUID || node.Type == OSDType.String) { return(new LSL_String(node.AsString())); } if (node.Type == OSDType.Array) { LSL_List resp = new LSL_List(); OSDArray ar = node as OSDArray; foreach (OSD o in ar) { resp.Add(ParseJsonNode(o)); } return(resp); } if (node.Type == OSDType.Map) { LSL_List resp = new LSL_List(); OSDMap ar = node as OSDMap; foreach (KeyValuePair <string, OSD> o in ar) { resp.Add(new LSL_String(o.Key)); resp.Add(ParseJsonNode(o.Value)); } return(resp); } throw new Exception(ScriptBaseClass.JSON_INVALID); }
public static object SerializeLisp(OSD osd) { switch (osd.Type) { case OSDType.Unknown: throw new InvalidCastException(); case OSDType.Boolean: return osd.AsBoolean(); case OSDType.Integer: return osd.AsInteger(); case OSDType.Real: return osd.AsReal(); case OSDType.String: return osd.AsString(); case OSDType.Date: return osd.AsDate(); case OSDType.URI: return osd.AsUri(); case OSDType.UUID: return osd.AsUUID(); case OSDType.Binary: return osd.AsBinary(); case OSDType.Array: OSDArray args = (OSDArray) osd; Cons ret = null; for (int i = args.Count - 1; i >= 0; --i) { ret = new Cons(args[i], ret); } return ret; case OSDType.Map: Cons list = null; OSDMap map = (OSDMap) osd; foreach (KeyValuePair<string, OSD> kvp in map) { Cons kv = new Cons(kvp.Key, new Cons(SerializeLisp(kvp.Value))); list = new Cons(kv,list); } return Cons.Reverse(list); default: return osd; } }
private Object osdToObject(OSD decoded) { if ( decoded is OSDString ) { return (string) decoded.AsString(); } else if ( decoded is OSDInteger ) { return (int) decoded.AsInteger(); } else if ( decoded is OSDReal ) { return (float) decoded.AsReal(); } else if ( decoded is OSDBoolean ) { return (bool) decoded.AsBoolean(); } else if ( decoded is OSDMap ) { return osdToHashtable((OSDMap) decoded); } else if ( decoded is OSDArray ) { return osdToArray((OSDArray) decoded); } else { return null; } }
public void DeserializeLLSDSample() { OSD theSD = null; OSDMap map = null; OSD tempSD = null; OSDUUID tempUUID = null; OSDString tempStr = null; OSDReal tempReal = null; String testSD = @"<?xml version='1.0' encoding='UTF-8'?> <llsd> <map> <key>region_id</key> <uuid>67153d5b-3659-afb4-8510-adda2c034649</uuid> <key>scale</key> <string>one minute</string> <key>simulator statistics</key> <map> <key>time dilation</key> <real>0.9878624</real> <key>sim fps</key> <real>44.38898</real> <key>agent updates per second</key> <real>nan</real> <key>total task count</key> <real>4</real> <key>active task count</key> <real>0</real> <key>pending uploads</key> <real>0.0001096525</real> </map> </map> </llsd>"; //Deserialize the string byte[] bytes = Encoding.UTF8.GetBytes(testSD); theSD = OSDParser.DeserializeLLSDXml(bytes); //Confirm the contents Assert.IsNotNull(theSD); Assert.IsTrue(theSD is OSDMap); Assert.IsTrue(theSD.Type == OSDType.Map); map = (OSDMap)theSD; tempSD = map["region_id"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDUUID); Assert.IsTrue(tempSD.Type == OSDType.UUID); tempUUID = (OSDUUID)tempSD; Assert.AreEqual(new UUID("67153d5b-3659-afb4-8510-adda2c034649"), tempUUID.AsUUID()); tempSD = map["scale"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDString); Assert.IsTrue(tempSD.Type == OSDType.String); tempStr = (OSDString)tempSD; Assert.AreEqual("one minute", tempStr.AsString()); tempSD = map["simulator statistics"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDMap); Assert.IsTrue(tempSD.Type == OSDType.Map); map = (OSDMap)tempSD; tempSD = map["time dilation"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDReal); Assert.IsTrue(tempSD.Type == OSDType.Real); tempReal = (OSDReal)tempSD; Assert.AreEqual(0.9878624d, tempReal.AsReal()); //TODO - figure out any relevant rounding variability for 64 bit reals tempSD = map["sim fps"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDReal); Assert.IsTrue(tempSD.Type == OSDType.Real); tempReal = (OSDReal)tempSD; Assert.AreEqual(44.38898d, tempReal.AsReal()); tempSD = map["agent updates per second"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDReal); Assert.IsTrue(tempSD.Type == OSDType.Real); tempReal = (OSDReal)tempSD; Assert.AreEqual(Double.NaN, tempSD.AsReal()); tempSD = map["total task count"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDReal); Assert.IsTrue(tempSD.Type == OSDType.Real); tempReal = (OSDReal)tempSD; Assert.AreEqual(4.0d, tempReal.AsReal()); tempSD = map["active task count"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDReal); Assert.IsTrue(tempSD.Type == OSDType.Real); tempReal = (OSDReal)tempSD; Assert.AreEqual(0.0d, tempReal.AsReal()); tempSD = map["pending uploads"]; Assert.IsNotNull(tempSD); Assert.IsTrue(tempSD is OSDReal); Assert.IsTrue(tempSD.Type == OSDType.Real); tempReal = (OSDReal)tempSD; Assert.AreEqual(0.0001096525d, tempReal.AsReal()); }
private static JsonData SerializeJsonNoDefaults(OSD osd) { switch (osd.Type) { case OSDType.Boolean: bool b = osd.AsBoolean(); if (!b) return null; return new JsonData(b); case OSDType.Integer: int v = osd.AsInteger(); if (v == 0) return null; return new JsonData(v); case OSDType.Real: double d = osd.AsReal(); if (d == 0.0d) return null; return new JsonData(d); case OSDType.String: case OSDType.Date: case OSDType.URI: string str = osd.AsString(); if (String.IsNullOrEmpty(str)) return null; return new JsonData(str); case OSDType.UUID: UUID uuid = osd.AsUUID(); if (uuid == UUID.Zero) return null; return new JsonData(uuid.ToString()); case OSDType.Binary: byte[] binary = osd.AsBinary(); if (binary == Utils.EmptyBytes) return null; JsonData jsonbinarray = new JsonData(); jsonbinarray.SetJsonType(JsonType.Array); for (int i = 0; i < binary.Length; i++) jsonbinarray.Add(new JsonData(binary[i])); return jsonbinarray; case OSDType.Array: JsonData jsonarray = new JsonData(); jsonarray.SetJsonType(JsonType.Array); OSDArray array = (OSDArray)osd; for (int i = 0; i < array.Count; i++) jsonarray.Add(SerializeJson(array[i])); return jsonarray; case OSDType.Map: JsonData jsonmap = new JsonData(); jsonmap.SetJsonType(JsonType.Object); OSDMap map = (OSDMap)osd; foreach (KeyValuePair<string, OSD> kvp in map) { JsonData data = SerializeJsonNoDefaults(kvp.Value); if (data != null) jsonmap[kvp.Key] = data; } return jsonmap; case OSDType.Unknown: default: return null; } }
public static JsonData SerializeJson(OSD osd) { switch (osd.Type) { case OSDType.Boolean: return new JsonData(osd.AsBoolean()); case OSDType.Integer: return new JsonData(osd.AsInteger()); case OSDType.Real: return new JsonData(osd.AsReal()); case OSDType.String: return new JsonData(osd.AsString()); case OSDType.Date: return new JsonData("date::" + osd.AsString()); case OSDType.URI: return new JsonData("uri::" + osd.AsString()); case OSDType.UUID: return new JsonData("uuid::" + osd.AsString()); case OSDType.Binary: return new JsonData("b64::" + Convert.ToBase64String(osd.AsBinary())); case OSDType.Array: JsonData jsonarray = new JsonData(); jsonarray.SetJsonType(JsonType.Array); OSDArray array = (OSDArray)osd; for (int i = 0; i < array.Count; i++) jsonarray.Add(SerializeJson(array[i])); return jsonarray; case OSDType.Map: JsonData jsonmap = new JsonData(); jsonmap.SetJsonType(JsonType.Object); OSDMap map = (OSDMap)osd; foreach (KeyValuePair<string, OSD> kvp in map) jsonmap[kvp.Key] = SerializeJson(kvp.Value); return jsonmap; case OSDType.Unknown: default: return new JsonData(); } }
private object ParseJsonNode(OSD node) { if (node.Type == OSDType.Integer) return new LSL_Integer(node.AsInteger()); if (node.Type == OSDType.Boolean) return new LSL_Integer(node.AsBoolean() ? 1 : 0); if (node.Type == OSDType.Real) return new LSL_Float(node.AsReal()); if (node.Type == OSDType.UUID || node.Type == OSDType.String) return new LSL_String(node.AsString()); if (node.Type == OSDType.Array) { LSL_List resp = new LSL_List(); OSDArray ar = node as OSDArray; foreach (OSD o in ar) resp.Add(ParseJsonNode(o)); return resp; } if (node.Type == OSDType.Map) { LSL_List resp = new LSL_List(); OSDMap ar = node as OSDMap; foreach (KeyValuePair<string, OSD> o in ar) { resp.Add(new LSL_String(o.Key)); resp.Add(ParseJsonNode(o.Value)); } return resp; } throw new Exception(ScriptBaseClass.JSON_INVALID); }
/// <summary> /// Set member values by decoding out of propertyData. Should only /// be called in initialization time (e.g. from constructor). /// </summary> /// <param name="propertyData"></param> private void FromOSDArray(OSDArray propertyData) { Property = (SyncableProperties.Type)(propertyData[0].AsInteger()); LastUpdateTimeStamp = propertyData[1].AsLong(); LastUpdateSyncID = propertyData[2].AsString(); OSD value = propertyData[3]; switch (Property) { /////////////////////////////////////// // Complex structure properties /////////////////////////////////////// case SyncableProperties.Type.AgentCircuitData: case SyncableProperties.Type.AvatarAppearance: LastUpdateValue = (OSDMap)value; break; case SyncableProperties.Type.Animations: LastUpdateValue = (OSDArray)value; break; //////////////////////////// // Integer/enum type properties //////////////////////////// case SyncableProperties.Type.CreationDate: // int case SyncableProperties.Type.LinkNum: // int case SyncableProperties.Type.OwnershipCost: // int case SyncableProperties.Type.SalePrice: // int case SyncableProperties.Type.ScriptAccessPin: // int case SyncableProperties.Type.AggregateScriptEvents: // enum case SyncableProperties.Type.Flags: // enum case SyncableProperties.Type.LocalFlags: // enum case SyncableProperties.Type.PresenceType: // enum LastUpdateValue = value.AsInteger(); break; case SyncableProperties.Type.ClickAction: case SyncableProperties.Type.Material: case SyncableProperties.Type.ObjectSaleType: LastUpdateValue = (byte)value.AsInteger(); break; //////////////////////////// // Boolean type properties //////////////////////////// case SyncableProperties.Type.AllowedDrop: case SyncableProperties.Type.IsAttachment: case SyncableProperties.Type.PassTouches: case SyncableProperties.Type.VolumeDetectActive: case SyncableProperties.Type.Flying: case SyncableProperties.Type.IsColliding: case SyncableProperties.Type.CollidingGround: case SyncableProperties.Type.Kinematic: case SyncableProperties.Type.IsSelected: case SyncableProperties.Type.AllowMovement: LastUpdateValue = value.AsBoolean(); break; //////////////////////////// // Vector3 type properties //////////////////////////// case SyncableProperties.Type.AngularVelocity: case SyncableProperties.Type.AttachedPos: case SyncableProperties.Type.GroupPosition: case SyncableProperties.Type.OffsetPosition: case SyncableProperties.Type.Scale: case SyncableProperties.Type.SitTargetPosition: case SyncableProperties.Type.SitTargetPositionLL: case SyncableProperties.Type.SOP_Acceleration: case SyncableProperties.Type.Velocity: case SyncableProperties.Type.Force: case SyncableProperties.Type.PA_Acceleration: case SyncableProperties.Type.PA_Velocity: case SyncableProperties.Type.PA_TargetVelocity: case SyncableProperties.Type.Position: case SyncableProperties.Type.RotationalVelocity: case SyncableProperties.Type.Size: case SyncableProperties.Type.Torque: case SyncableProperties.Type.AbsolutePosition: LastUpdateValue = value.AsVector3(); break; //////////////////////////// // UUID type properties //////////////////////////// case SyncableProperties.Type.AttachedAvatar: case SyncableProperties.Type.CollisionSound: case SyncableProperties.Type.CreatorID: case SyncableProperties.Type.FolderID: case SyncableProperties.Type.GroupID: case SyncableProperties.Type.LastOwnerID: case SyncableProperties.Type.OwnerID: case SyncableProperties.Type.Sound: LastUpdateValue = value.AsUUID(); break; //////////////////////////// // UInt type properties //////////////////////////// case SyncableProperties.Type.LocalId: case SyncableProperties.Type.AttachmentPoint: case SyncableProperties.Type.BaseMask: case SyncableProperties.Type.Category: case SyncableProperties.Type.EveryoneMask: case SyncableProperties.Type.GroupMask: case SyncableProperties.Type.InventorySerial: case SyncableProperties.Type.NextOwnerMask: case SyncableProperties.Type.OwnerMask: case SyncableProperties.Type.AgentControlFlags: case SyncableProperties.Type.ParentId: LastUpdateValue = value.AsUInteger(); break; //////////////////////////// // Float type properties //////////////////////////// case SyncableProperties.Type.CollisionSoundVolume: case SyncableProperties.Type.Buoyancy: LastUpdateValue = (float)value.AsReal(); break; //////////////////////////// // String type properties //////////////////////////// case SyncableProperties.Type.Color: case SyncableProperties.Type.CreatorData: case SyncableProperties.Type.Description: case SyncableProperties.Type.MediaUrl: case SyncableProperties.Type.Name: case SyncableProperties.Type.RealRegion: case SyncableProperties.Type.Shape: case SyncableProperties.Type.SitName: case SyncableProperties.Type.TaskInventory: case SyncableProperties.Type.Text: case SyncableProperties.Type.TouchName: LastUpdateValue = value.AsString(); break; //////////////////////////// // byte[] (binary data) type properties //////////////////////////// case SyncableProperties.Type.ParticleSystem: case SyncableProperties.Type.TextureAnimation: LastUpdateValue = value.AsBinary(); break; //////////////////////////// // Quaternion type properties //////////////////////////// case SyncableProperties.Type.RotationOffset: case SyncableProperties.Type.SitTargetOrientation: case SyncableProperties.Type.SitTargetOrientationLL: case SyncableProperties.Type.Orientation: case SyncableProperties.Type.Rotation: LastUpdateValue = value.AsQuaternion(); break; default: DebugLog.WarnFormat("[SYNCED PROPERTY] FromOSDArray: No handler for property {0} ", Property); break; } }