From 5f4b80c2985a60eeea04fc0cd2839b5b43dd502d Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Mon, 20 Apr 2009 23:45:31 +0000 Subject: [PATCH] Added OSDParser.Deserialize(), a new command to automatically try and guess the content type for LLSD data and decode it. Errors on the side of JSON, so avoid using this function for libomv SL functions (which typically use XML) git-svn-id: http://libopenmetaverse.googlecode.com/svn/libopenmetaverse/trunk@2638 52acb1d6-8a22-11de-b505-999d5b087335 --- .../StructuredData.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/OpenMetaverse.StructuredData/StructuredData.cs b/OpenMetaverse.StructuredData/StructuredData.cs index f034ec77..8b877bdb 100644 --- a/OpenMetaverse.StructuredData/StructuredData.cs +++ b/OpenMetaverse.StructuredData/StructuredData.cs @@ -27,6 +27,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; using System.Reflection; using System.Text; @@ -1091,4 +1092,49 @@ namespace OpenMetaverse.StructuredData #endregion IList Implementation } + + public partial class OSDParser + { + const string LLSD_BINARY_HEADER = ""; + const string LLSD_XML_HEADER = ""; + const string LLSD_XML_ALT_HEADER = ""; + + public static OSD Deserialize(byte[] data) + { + string header = Encoding.ASCII.GetString(data, 0, data.Length >= 14 ? 14 : data.Length); + + if (header.StartsWith(LLSD_BINARY_HEADER)) + return DeserializeLLSDBinary(data); + else if (header.StartsWith(LLSD_XML_HEADER)) + return DeserializeLLSDXml(data); + else if (header.StartsWith(LLSD_XML_ALT_HEADER)) + return DeserializeLLSDXml(data); + else + return DeserializeJson(Encoding.UTF8.GetString(data)); + } + + public static OSD Deserialize(Stream stream) + { + if (stream.CanSeek) + { + byte[] headerData = new byte[14]; + stream.Read(headerData, 0, 14); + stream.Seek(0, SeekOrigin.Begin); + string header = Encoding.ASCII.GetString(headerData); + + if (header.StartsWith(LLSD_BINARY_HEADER)) + return DeserializeLLSDBinary(stream); + else if (header.StartsWith(LLSD_XML_HEADER)) + return DeserializeLLSDXml(stream); + else if (header.StartsWith(LLSD_XML_ALT_HEADER)) + return DeserializeLLSDXml(stream); + else + return DeserializeJson(stream); + } + else + { + throw new OSDException("Cannot deserialize structured data from unseekable streams"); + } + } + } }