Skip to content

Commit

Permalink
Merge pull request #6 from ricaun-io/develop
Browse files Browse the repository at this point in the history
Version 1.0.3
  • Loading branch information
ricaun authored Oct 10, 2022
2 parents 98c2757 + 93c5c74 commit 1c9ec5e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 57 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.3] / 2022-10-10
### Features
- Add `ObjectJsonConverter` abstraction

## [1.0.2] / 2022-10-03
- Update `Newtonsoft.Json` to `Version="9.*"`

Expand All @@ -18,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- First Release

[vNext]: ../../compare/1.0.0...HEAD
[1.0.3]: ../../compare/1.0.2...1.0.3
[1.0.2]: ../../compare/1.0.1...1.0.2
[1.0.1]: ../../compare/1.0.0...1.0.1
[1.0.0]: ../../compare/1.0.0
35 changes: 9 additions & 26 deletions ricaun.Revit.ExtensibleJson/Converters/ElementIdConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,31 @@ namespace ricaun.Revit.ExtensibleJson.Converters
/// <summary>
/// Convert Json to <see cref="Autodesk.Revit.DB.ElementId"/>
/// </summary>
public class ElementIdConverter : JsonConverter
public class ElementIdConverter : ObjectJsonConverter<ElementId>
{
private const string ElementIdKey = "IntegerValue";

/// <summary>
/// Check type equal to <see cref="Autodesk.Revit.DB.ElementId"/>
/// Read
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ElementId);
}

/// <summary>
/// ReadJson
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <param name="value"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
public override ElementId Read(JObject value)
{
JObject jObject = JObject.Load(reader);

int id = (int)jObject[ElementIdKey];
int id = (int)value[ElementIdKey];
ElementId elementId = new ElementId(id);

return elementId;
}

/// <summary>
/// WriteJson
/// Write
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
/// <returns></returns>
public override object Write(ElementId value)
{
JToken t = JToken.FromObject(value);
t.WriteTo(writer);
return value;
}
}
}
62 changes: 62 additions & 0 deletions ricaun.Revit.ExtensibleJson/Converters/ObjectJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace ricaun.Revit.ExtensibleJson.Converters
{
/// <summary>
/// ObjectJsonConverter
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class ObjectJsonConverter<T> : JsonConverter
{
/// <summary>
/// Convert <paramref name="value"/> to <typeparamref name="T"/>
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public abstract T Read(JObject value);

/// <summary>
/// Convert <paramref name="value"/> to object
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public abstract object Write(T value);

/// <summary>
/// Check type equal to <typeparamref name="T"/>
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}

/// <summary>
/// ReadJson
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return Read(JObject.Load(reader));
}

/// <summary>
/// WriteJson
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken.FromObject(Write((T)value))
.WriteTo(writer);
}
}
}
54 changes: 25 additions & 29 deletions ricaun.Revit.ExtensibleJson/Converters/XYZConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,49 @@ namespace ricaun.Revit.ExtensibleJson.Converters
/// <summary>
/// Convert Json to <see cref="Autodesk.Revit.DB.XYZ"/>
/// </summary>
public class XYZConverter : JsonConverter
public class XYZConverter : ObjectJsonConverter<XYZ>
{
private const string XKey = "X";
private const string YKey = "Y";
private const string ZKey = "Z";

/// <summary>
/// Check type equal to <see cref="Autodesk.Revit.DB.XYZ"/>
/// Read
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(XYZ);
}

/// <summary>
/// ReadJson
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <param name="value"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
public override XYZ Read(JObject value)
{
JObject jObject = JObject.Load(reader);

double x = (double)jObject[XKey];
double y = (double)jObject[YKey];
double z = (double)jObject[ZKey];
double x = (double)value[XKey];
double y = (double)value[YKey];
double z = (double)value[ZKey];
XYZ xyz = new XYZ(x, y, z);

return xyz;
}

/// <summary>
/// WriteJson
/// Write
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
/// <returns></returns>
public override object Write(XYZ value)
{
JToken t = JToken.FromObject(value);
t.WriteTo(writer);
return value;
}



///// <summary>
///// WriteJson
///// </summary>
///// <param name="writer"></param>
///// <param name="value"></param>
///// <param name="serializer"></param>
//public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
//{
// JToken t = JToken.FromObject(value);
// t.WriteTo(writer);
//}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<OutputType>Library</OutputType>
<PlatformTarget>AnyCPU</PlatformTarget>
<UseWPF>true</UseWPF>
Expand Down Expand Up @@ -77,7 +76,7 @@

<PropertyGroup>
<PackageId>ricaun.Revit.ExtensibleJson</PackageId>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<ProjectGuid>{c94c5551-b010-4908-874c-0732abf1d512}</ProjectGuid>
</PropertyGroup>

Expand Down

0 comments on commit 1c9ec5e

Please sign in to comment.