Switched to service stack json. Added more api enhancements.

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti
2012-07-15 16:27:07 -04:00
parent 2e03cb0916
commit baedafbeb9
27 changed files with 370 additions and 142 deletions

View File

@@ -1,55 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO;
namespace MediaBrowser.Common.Json
{
public class JsonSerializer
{
public static void Serialize<T>(T o, Stream stream)
public static void SerializeToStream<T>(T o, Stream stream)
{
using (StreamWriter streamWriter = new StreamWriter(stream))
{
using (Newtonsoft.Json.JsonTextWriter writer = new Newtonsoft.Json.JsonTextWriter(streamWriter))
{
var settings = new Newtonsoft.Json.JsonSerializerSettings()
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};
Newtonsoft.Json.JsonSerializer.Create(settings).Serialize(writer, o);
}
}
Configure();
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(o, stream);
}
public static void Serialize<T>(T o, string file)
public static void SerializeToFile<T>(T o, string file)
{
Configure();
using (StreamWriter streamWriter = new StreamWriter(file))
{
using (Newtonsoft.Json.JsonTextWriter writer = new Newtonsoft.Json.JsonTextWriter(streamWriter))
{
var settings = new Newtonsoft.Json.JsonSerializerSettings()
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};
Newtonsoft.Json.JsonSerializer.Create(settings).Serialize(writer, o);
}
ServiceStack.Text.JsonSerializer.SerializeToWriter<T>(o, streamWriter);
}
}
public static T Deserialize<T>(string file)
public static T DeserializeFromFile<T>(string file)
{
using (StreamReader streamReader = new StreamReader(file))
Configure();
using (Stream stream = File.OpenRead(file))
{
using (Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(streamReader))
{
return Newtonsoft.Json.JsonSerializer.Create(new Newtonsoft.Json.JsonSerializerSettings() { }).Deserialize<T>(reader);
}
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
}
}
public static T DeserializeFromStream<T>(Stream stream)
{
Configure();
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
}
public static T DeserializeFromString<T>(string data)
{
Configure();
return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(data);
}
private static void Configure()
{
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
ServiceStack.Text.JsConfig.IncludeNullValues = false;
ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Text;
using System.Threading;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Common.Logging
{

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Common.Logging
{

View File

@@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Logging
{
[Flags]
public enum LogSeverity
{
None = 0,
Debug = 1,
Info = 2,
Warning = 4,
Error = 8
}
}

View File

@@ -30,9 +30,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.4.5.7\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Text">
<HintPath>..\packages\ServiceStack.Text.3.8.5\lib\net35\ServiceStack.Text.dll</HintPath>
</Reference>
@@ -50,6 +47,7 @@
<ItemGroup>
<Compile Include="Events\GenericItemEventArgs.cs" />
<Compile Include="Json\JsonSerializer.cs" />
<Compile Include="Model\DictionaryBaseItem.cs" />
<Compile Include="Net\CollectionExtensions.cs" />
<Compile Include="Net\Handlers\BaseEmbeddedResourceHandler.cs" />
<Compile Include="Net\Handlers\JsonHandler.cs" />
@@ -63,7 +61,6 @@
<Compile Include="Logging\FileLogger.cs" />
<Compile Include="Logging\Logger.cs" />
<Compile Include="Logging\LogRow.cs" />
<Compile Include="Logging\LogSeverity.cs" />
<Compile Include="Plugins\BasePlugin.cs" />
<Compile Include="Plugins\PluginController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -71,6 +68,12 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj">
<Project>{9b1ddd79-5134-4df3-ace3-d1957a7350d8}</Project>
<Name>MediaBrowser.Model</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -0,0 +1,227 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MediaBrowser.Model.Entities;
using System.IO;
using MediaBrowser.Common.Json;
namespace MediaBrowser.Common.Model
{
public class DictionaryBaseItem : BaseItem
{
private Dictionary<string, object> Dictionary { get; set; }
public DictionaryBaseItem(Dictionary<string, object> dictionary)
{
Dictionary = dictionary;
}
public override string Name
{
get
{
return GetString("Name");
}
set
{
SetValue("Name", value);
}
}
public override string ArtImagePath
{
get
{
return GetString("ArtImagePath");
}
set
{
SetValue("ArtImagePath", value);
}
}
public override string AspectRatio
{
get
{
return GetString("AspectRatio");
}
set
{
SetValue("AspectRatio", value);
}
}
public override string BannerImagePath
{
get
{
return GetString("BannerImagePath");
}
set
{
SetValue("BannerImagePath", value);
}
}
public override string CustomPin
{
get
{
return GetString("CustomPin");
}
set
{
SetValue("CustomPin", value);
}
}
public override string CustomRating
{
get
{
return GetString("CustomRating");
}
set
{
SetValue("CustomRating", value);
}
}
public override string DisplayMediaType
{
get
{
return GetString("DisplayMediaType");
}
set
{
SetValue("DisplayMediaType", value);
}
}
public override string LogoImagePath
{
get
{
return GetString("LogoImagePath");
}
set
{
SetValue("LogoImagePath", value);
}
}
public override string OfficialRating
{
get
{
return GetString("OfficialRating");
}
set
{
SetValue("OfficialRating", value);
}
}
public override string Overview
{
get
{
return GetString("Overview");
}
set
{
SetValue("Overview", value);
}
}
public override string Path
{
get
{
return GetString("Path");
}
set
{
SetValue("Path", value);
}
}
public override string PrimaryImagePath
{
get
{
return GetString("PrimaryImagePath");
}
set
{
SetValue("PrimaryImagePath", value);
}
}
public override string SortName
{
get
{
return GetString("SortName");
}
set
{
SetValue("SortName", value);
}
}
public override string Tagline
{
get
{
return GetString("Tagline");
}
set
{
SetValue("Tagline", value);
}
}
public override string TrailerUrl
{
get
{
return GetString("TrailerUrl");
}
set
{
SetValue("TrailerUrl", value);
}
}
private string GetString(string name)
{
return Dictionary[name] as string;
}
private void SetValue<T>(string name, T value)
{
Dictionary[name] = value;
}
public static DictionaryBaseItem FromApiOutput(Stream stream)
{
Dictionary<string,object> data = JsonSerializer.DeserializeFromStream<Dictionary<string, object>>(stream);
if (data.ContainsKey("BaseItem"))
{
string baseItem = data["BaseItem"] as string;
data = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(baseItem);
return new DictionaryBaseItem(data);
}
return new DictionaryBaseItem(data);
}
}
}

View File

@@ -29,7 +29,7 @@ namespace MediaBrowser.Common.Net.Handlers
{
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, false))
{
JsonSerializer.Serialize(ObjectToSerialize, gzipStream);
JsonSerializer.SerializeToStream(ObjectToSerialize, gzipStream);
}
}
}

View File

@@ -40,7 +40,7 @@ namespace MediaBrowser.Common.Plugins
return new TConfigurationType();
}
return JsonSerializer.Deserialize<TConfigurationType>(ConfigurationPath);
return JsonSerializer.DeserializeFromFile<TConfigurationType>(ConfigurationPath);
}
}

View File

@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="4.5.7" targetFramework="net45" />
<package id="Rx-Main" version="1.0.11226" targetFramework="net45" />
<package id="ServiceStack.Text" version="3.8.5" targetFramework="net45" />
</packages>