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,6 +1,7 @@
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Common.Json;
namespace MediaBrowser.Api.HttpHandlers
{
@ -21,24 +22,19 @@ namespace MediaBrowser.Api.HttpHandlers
public static object GetSerializationObject(BaseItem item, bool includeChildren)
{
Folder folder = item as Folder;
if (includeChildren && folder != null)
if (includeChildren && item.IsFolder)
{
Folder folder = item as Folder;
return new
{
BaseItem = item,
Children = folder.Children,
Type = item.GetType().Name
Children = folder.Children
};
}
else
{
return new
{
BaseItem = item,
Type = item.GetType().Name
};
return item;
}
}

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

@ -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>

View file

@ -202,7 +202,7 @@ namespace MediaBrowser.Controller
return new Configuration();
}
return JsonSerializer.Deserialize<Configuration>(file);
return JsonSerializer.DeserializeFromFile<Configuration>(file);
}
public void ReloadItem(BaseItem item)

View file

@ -30,10 +30,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.4.5.7\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Reactive">

View file

@ -41,7 +41,7 @@ namespace MediaBrowser.Controller
{
string file = Path.Combine(path, "user.js");
return JsonSerializer.Deserialize<User>(file);
return JsonSerializer.DeserializeFromFile<User>(file);
}
public void CreateUser(User user)
@ -54,7 +54,7 @@ namespace MediaBrowser.Controller
Directory.CreateDirectory(userFolder);
JsonSerializer.Serialize(user, Path.Combine(userFolder, "user.js"));
JsonSerializer.SerializeToFile(user, Path.Combine(userFolder, "user.js"));
}
}
}

View file

@ -1,5 +1,4 @@
<?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" />
</packages>

View file

@ -1,4 +1,4 @@
using MediaBrowser.Common.Logging;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Model.Configuration
{
@ -11,7 +11,7 @@ namespace MediaBrowser.Model.Configuration
public Configuration()
{
HttpServerPortNumber = 8096;
LogSeverity = Common.Logging.LogSeverity.Info;
LogSeverity = LogSeverity.Info;
}
}
}

View file

@ -1,65 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Runtime.Serialization;
namespace MediaBrowser.Model.Entities
{
public abstract class BaseItem
{
public string Name { get; set; }
public string SortName { get; set; }
public virtual string Name { get; set; }
public virtual string SortName { get; set; }
public Guid Id { get; set; }
public virtual Guid Id { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public virtual DateTime DateCreated { get; set; }
public string Path { get; set; }
public virtual DateTime DateModified { get; set; }
[JsonIgnore]
public virtual string Path { get; set; }
[IgnoreDataMember]
public Folder Parent { get; set; }
public string PrimaryImagePath { get; set; }
public string LogoImagePath { get; set; }
public string ArtImagePath { get; set; }
public string ThumbnailImagePath { get; set; }
public string BannerImagePath { get; set; }
public virtual string PrimaryImagePath { get; set; }
public virtual string LogoImagePath { get; set; }
public virtual string ArtImagePath { get; set; }
public virtual string ThumbnailImagePath { get; set; }
public virtual string BannerImagePath { get; set; }
public IEnumerable<string> BackdropImagePaths { get; set; }
public virtual IEnumerable<string> BackdropImagePaths { get; set; }
public string OfficialRating { get; set; }
public virtual string OfficialRating { get; set; }
public string CustomRating { get; set; }
public string CustomPin { get; set; }
public virtual string CustomRating { get; set; }
public virtual string CustomPin { get; set; }
public string Overview { get; set; }
public string Tagline { get; set; }
public virtual string Overview { get; set; }
public virtual string Tagline { get; set; }
[JsonIgnore]
public IEnumerable<PersonInfo> People { get; set; }
[IgnoreDataMember]
public virtual IEnumerable<PersonInfo> People { get; set; }
public IEnumerable<string> Studios { get; set; }
public virtual IEnumerable<string> Studios { get; set; }
public IEnumerable<string> Genres { get; set; }
public virtual IEnumerable<string> Genres { get; set; }
public string DisplayMediaType { get; set; }
public virtual string DisplayMediaType { get; set; }
public float? UserRating { get; set; }
public TimeSpan? RunTime { get; set; }
public virtual float? UserRating { get; set; }
public virtual TimeSpan? RunTime { get; set; }
public string AspectRatio { get; set; }
public int? ProductionYear { get; set; }
public virtual string AspectRatio { get; set; }
public virtual int? ProductionYear { get; set; }
public IEnumerable<Video> LocalTrailers { get; set; }
public string TrailerUrl { get; set; }
public virtual IEnumerable<Video> LocalTrailers { get; set; }
public virtual string TrailerUrl { get; set; }
public override string ToString()
{
return Name;
}
/// <summary>
/// This is strictly to enhance json output, until I can find a way to customize service stack to add this without having to use a property
/// </summary>
public virtual bool IsFolder
{
get
{
return false;
}
}
/// <summary>
/// This is strictly to enhance json output, until I can find a way to customize service stack to add this without having to use a property
/// </summary>
public string Type
{
get
{
return GetType().Name;
}
}
}
}

View file

@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using System.Linq;
using System.Runtime.Serialization;
namespace MediaBrowser.Model.Entities
{
@ -20,10 +18,18 @@ namespace MediaBrowser.Model.Entities
}
}
[JsonIgnore]
public override bool IsFolder
{
get
{
return true;
}
}
[IgnoreDataMember]
public BaseItem[] Children { get; set; }
[JsonIgnore]
[IgnoreDataMember]
public IEnumerable<Folder> FolderChildren { get { return Children.OfType<Folder>(); } }
public Folder GetFolderByName(string name)

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Logging
namespace MediaBrowser.Model.Logging
{
[Flags]
public enum LogSeverity

View file

@ -30,11 +30,9 @@
<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="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -48,20 +46,12 @@
<Compile Include="Entities\BaseItem.cs" />
<Compile Include="Entities\Folder.cs" />
<Compile Include="Entities\PlaybackStatus.cs" />
<Compile Include="Logging\LogSeverity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Users\User.cs" />
<Compile Include="Users\UserItemData.cs" />
<Compile Include="Entities\Video.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj">
<Project>{9142eefa-7570-41e1-bfcc-468bb571af2f}</Project>
<Name>MediaBrowser.Common</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

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="4.5.7" targetFramework="net45" />
</packages>

View file

@ -49,6 +49,10 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj">
<Project>{9142eefa-7570-41e1-bfcc-468bb571af2f}</Project>
<Name>MediaBrowser.Common</Name>
</ProjectReference>
<ProjectReference Include="..\MediaBrowser.Controller\MediaBrowser.Controller.csproj">
<Project>{17e1f4e6-8abd-4fe5-9ecf-43d4b6087ba2}</Project>
<Name>MediaBrowser.Controller</Name>

View file

@ -41,6 +41,7 @@ namespace MediaBrowser.Program
var time = DateTime.Now - now;
Console.WriteLine("Done in " + time.TotalSeconds + " seconds");
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}

View file

@ -8,6 +8,6 @@ namespace MediaBrowser.TV.Entities
{
public string SeasonNumber { get; set; }
public string EpisodeNumber { get; set; }
public string FirstAired { get; set; }
public DateTime? FirstAired { get; set; }
}
}

View file

@ -1,7 +1,6 @@
using MediaBrowser.Model.Entities;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Runtime.Serialization;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.TV.Entities
{
@ -10,7 +9,7 @@ namespace MediaBrowser.TV.Entities
/// <summary>
/// Store these to reduce disk access in Episode Resolver
/// </summary>
[JsonIgnore]
[IgnoreDataMember]
public IEnumerable<string> MetadataFiles { get; set; }
}
}

View file

@ -30,12 +30,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.4.5.7\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -69,9 +66,6 @@
<Name>MediaBrowser.Model</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>xcopy "$(TargetPath)" "$(SolutionDir)\ProgramData\Plugins\$(ProjectName)\" /y</PostBuildEvent>

View file

@ -37,15 +37,16 @@ namespace MediaBrowser.TV.Metadata
case "FirstAired":
{
item.FirstAired = reader.ReadElementContentAsString() ?? string.Empty;
string firstAired = reader.ReadElementContentAsString() ?? string.Empty;
if (!string.IsNullOrEmpty(item.FirstAired))
if (!string.IsNullOrEmpty(firstAired))
{
DateTime airDate;
int y = DateTime.TryParse(item.FirstAired, out airDate) ? airDate.Year : -1;
if (y > 1850)
if (DateTime.TryParse(firstAired, out airDate) && airDate.Year > 1850)
{
item.ProductionYear = y;
item.FirstAired = airDate;
item.ProductionYear = airDate.Year;
}
}

View file

@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using MediaBrowser.Controller.Events;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.TV.Entities;

View file

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="4.5.7" targetFramework="net45" />
</packages>