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
This commit is contained in:
John Hurliman
2009-04-20 23:45:31 +00:00
parent 3497eed168
commit 5f4b80c298

View File

@@ -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 = "<?LLSD/Binary?>";
const string LLSD_XML_HEADER = "<?LLSD/XML?>";
const string LLSD_XML_ALT_HEADER = "<llsd>";
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");
}
}
}
}