Browse Source

重写适合.net的参数序列化方法

qianyi
shanji 3 years ago
parent
commit
80f84f6bd4
  1. 4
      BBWY.1688SDK/BBWY.1688SDK.csproj
  2. 172
      BBWY.1688SDK/example/param/apiexample/ExampleFamily.cs
  3. 84
      BBWY.1688SDK/example/param/apiexample/ExampleFamilyPostParam.cs
  4. 116
      BBWY.1688SDK/example/param/apiexample/ExampleHouse.cs
  5. 2
      BBWY.1688SDK/serialize/Json2Deserializer.cs
  6. 107
      BBWY.1688SDK/serialize/Param2RequestSerializer.cs

4
BBWY.1688SDK/BBWY.1688SDK.csproj

@ -5,4 +5,8 @@
<RootNamespace>BBWY._1688SDK</RootNamespace> <RootNamespace>BBWY._1688SDK</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project> </Project>

172
BBWY.1688SDK/example/param/apiexample/ExampleFamily.cs

@ -9,124 +9,136 @@ using System.Text;
namespace com.alibaba.china.openapi.client.example.param.apiexample namespace com.alibaba.china.openapi.client.example.param.apiexample
{ {
[DataContract(Namespace = "com.alibaba.openapi.client")] [DataContract(Namespace = "com.alibaba.openapi.client")]
public class ExampleFamily public class ExampleFamily
{ {
[DataMember(Order = 1)] [DataMember(Order = 1)]
private int? familyNumber; private int? familyNumber;
/** /**
* @return * @return
*/ */
public int? getFamilyNumber() { public int? getFamilyNumber()
return familyNumber; {
} return familyNumber;
}
/**
* * /**
* <pre></pre> * *
* * <pre></pre>
*/ *
public void setFamilyNumber(int familyNumber) { */
this.familyNumber = familyNumber; public void setFamilyNumber(int familyNumber)
} {
this.familyNumber = familyNumber;
}
[DataMember(Order = 2)] [DataMember(Order = 2)]
private ExamplePerson father; private ExamplePerson father;
/** /**
* @return * @return
*/ */
public ExamplePerson getFather() { public ExamplePerson getFather()
return father; {
} return father;
}
/**
* * /**
* <pre></pre> * *
* * <pre></pre>
*/ *
public void setFather(ExamplePerson father) { */
this.father = father; public void setFather(ExamplePerson father)
} {
this.father = father;
}
[DataMember(Order = 3)] [DataMember(Order = 3)]
private ExamplePerson mother; private ExamplePerson mother;
/** /**
* @return * @return
*/ */
public ExamplePerson getMother() { public ExamplePerson getMother()
return mother; {
} return mother;
}
/**
* * /**
* <pre></pre> * *
* * <pre></pre>
*/ *
public void setMother(ExamplePerson mother) { */
this.mother = mother; public void setMother(ExamplePerson mother)
} {
this.mother = mother;
}
[DataMember(Order = 4)] [DataMember(Order = 4)]
private ExamplePerson[] children; private ExamplePerson[] children;
/** /**
* @return * @return
*/ */
public ExamplePerson[] getChildren() { public ExamplePerson[] getChildren()
return children; {
} return children;
}
/**
* * /**
* <pre></pre> * *
* * <pre></pre>
*/ *
public void setChildren(ExamplePerson[] children) { */
this.children = children; public void setChildren(ExamplePerson[] children)
} {
this.children = children;
}
[DataMember(Order = 5)] [DataMember(Order = 5)]
private ExampleCar[] ownedCars; private ExampleCar[] ownedCars;
/** /**
* @return * @return
*/ */
public ExampleCar[] getOwnedCars() { public ExampleCar[] getOwnedCars()
return ownedCars; {
} return ownedCars;
}
/**
* * /**
* <pre></pre> * *
* * <pre></pre>
*/ *
public void setOwnedCars(ExampleCar[] ownedCars) { */
this.ownedCars = ownedCars; public void setOwnedCars(ExampleCar[] ownedCars)
} {
this.ownedCars = ownedCars;
}
[DataMember(Order = 6)] [DataMember(Order = 6)]
private ExampleHouse myHouse; private ExampleHouse myHouse;
/** /**
* @return * @return
*/ */
public ExampleHouse getMyHouse() { public ExampleHouse getMyHouse()
return myHouse; {
} return myHouse;
}
/** /**
* * * *
* <pre></pre> * <pre></pre>
* *
*/ */
public void setMyHouse(ExampleHouse myHouse) { public void setMyHouse(ExampleHouse myHouse)
this.myHouse = myHouse; {
} this.myHouse = myHouse;
}
} }
} }

84
BBWY.1688SDK/example/param/apiexample/ExampleFamilyPostParam.cs

@ -9,67 +9,73 @@ using System.Text;
namespace com.alibaba.china.openapi.client.example.param.apiexample namespace com.alibaba.china.openapi.client.example.param.apiexample
{ {
[DataContract(Namespace = "com.alibaba.openapi.client")] [DataContract(Namespace = "com.alibaba.openapi.client")]
public class ExampleFamilyPostParam public class ExampleFamilyPostParam
{ {
[DataMember(Order = 1)] [DataMember(Order = 1)]
private ExampleFamily family; private ExampleFamily family;
/** /**
* @return Family对象信息 * @return Family对象信息
*/ */
public ExampleFamily getFamily() { public ExampleFamily getFamily()
return family; {
} return family;
}
/** /**
* Family对象信息 * * Family对象信息 *
* <pre></pre> * <pre></pre>
* *
*/ */
public void setFamily(ExampleFamily family) { public void setFamily(ExampleFamily family)
this.family = family; {
} this.family = family;
}
[DataMember(Order = 2)] [DataMember(Order = 2)]
private string comments; private string comments;
/** /**
* @return * @return
*/ */
public string getComments() { public string getComments()
return comments; {
} return comments;
}
/** /**
* * * *
* <pre></pre> * <pre></pre>
* *
*/ */
public void setComments(string comments) { public void setComments(string comments)
this.comments = comments; {
} this.comments = comments;
}
[DataMember(Order = 3)] [DataMember(Order = 3)]
private byte[] houseImg; private byte[] houseImg;
/** /**
* @return * @return
*/ */
public byte[] getHouseImg() { public byte[] getHouseImg()
return houseImg; {
} return houseImg;
}
/** /**
* * * *
* <pre></pre> * <pre></pre>
* *
*/ */
public void setHouseImg(byte[] houseImg) { public void setHouseImg(byte[] houseImg)
this.houseImg = houseImg; {
} this.houseImg = houseImg;
}
} }
} }

116
BBWY.1688SDK/example/param/apiexample/ExampleHouse.cs

@ -9,86 +9,94 @@ using System.Text;
namespace com.alibaba.china.openapi.client.example.param.apiexample namespace com.alibaba.china.openapi.client.example.param.apiexample
{ {
[DataContract(Namespace = "com.alibaba.openapi.client")] [DataContract(Namespace = "com.alibaba.openapi.client")]
public class ExampleHouse public class ExampleHouse
{ {
[DataMember(Order = 1)] [DataMember(Order = 1)]
private string location; private string location;
/** /**
* @return * @return
*/ */
public string getLocation() { public string getLocation()
return location; {
} return location;
}
/**
* * /**
* <pre></pre> * *
* * <pre></pre>
*/ *
public void setLocation(string location) { */
this.location = location; public void setLocation(string location)
} {
this.location = location;
}
[DataMember(Order = 2)] [DataMember(Order = 2)]
private int? areaSize; private int? areaSize;
/** /**
* @return * @return
*/ */
public int? getAreaSize() { public int? getAreaSize()
return areaSize; {
} return areaSize;
}
/**
* * /**
* <pre></pre> * *
* * <pre></pre>
*/ *
public void setAreaSize(int areaSize) { */
this.areaSize = areaSize; public void setAreaSize(int areaSize)
} {
this.areaSize = areaSize;
}
[DataMember(Order = 3)] [DataMember(Order = 3)]
private bool? rent; private bool? rent;
/** /**
* @return * @return
*/ */
public bool? getRent() { public bool? getRent()
return rent; {
} return rent;
}
/**
* * /**
* <pre></pre> * *
* * <pre></pre>
*/ *
public void setRent(bool rent) { */
this.rent = rent; public void setRent(bool rent)
} {
this.rent = rent;
}
[DataMember(Order = 4)] [DataMember(Order = 4)]
private int? rooms; private int? rooms;
/** /**
* @return * @return
*/ */
public int? getRooms() { public int? getRooms()
return rooms; {
} return rooms;
}
/** /**
* * * *
* <pre></pre> * <pre></pre>
* *
*/ */
public void setRooms(int rooms) { public void setRooms(int rooms)
this.rooms = rooms; {
} this.rooms = rooms;
}
} }
} }

2
BBWY.1688SDK/serialize/Json2Deserializer.cs

@ -38,7 +38,7 @@ namespace com.alibaba.openapi.client.serialize
{ {
istream.Position = 0; //istream.Position = 0;
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ErrorExceptionDesc)); DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ErrorExceptionDesc));
object resultObj = jsonSerializer.ReadObject(istream); object resultObj = jsonSerializer.ReadObject(istream);
ErrorExceptionDesc result = (ErrorExceptionDesc)resultObj; ErrorExceptionDesc result = (ErrorExceptionDesc)resultObj;

107
BBWY.1688SDK/serialize/Param2RequestSerializer.cs

@ -1,6 +1,7 @@
using com.alibaba.openapi.client.policy; using com.alibaba.openapi.client.policy;
using com.alibaba.openapi.client.primitive; using com.alibaba.openapi.client.primitive;
using com.alibaba.openapi.client.util; using com.alibaba.openapi.client.util;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -23,8 +24,9 @@ namespace com.alibaba.openapi.client.serialize
public Dictionary<String, Object> serialize(Object serializer) public Dictionary<String, Object> serialize(Object serializer)
{ {
Dictionary<String, Object> result = serializeNest(serializer); //Dictionary<String, Object> result = serializeNest(serializer);
return result; //return result;
return serializeParam(serializer);
} }
private Dictionary<String, Object> serializeNest(Object serializer) private Dictionary<String, Object> serializeNest(Object serializer)
@ -49,57 +51,78 @@ namespace com.alibaba.openapi.client.serialize
String tempName = upperFirstCharacter + piName.Substring(1); String tempName = upperFirstCharacter + piName.Substring(1);
if (!"ApiId".Equals(tempName)) if (!"ApiId".Equals(tempName))
{ {
MethodInfo mi = type.GetMethod("get" + tempName); MethodInfo mi = type.GetMethod("get" + tempName);
object value = mi.Invoke(serializer, null); object value = mi.Invoke(serializer, null);
if (value != null) if (value != null)
{
object trueValue = null;
if (fieldType.IsAssignableFrom(typeof(bool?))
||fieldType.IsAssignableFrom(typeof(byte?))
||fieldType.IsAssignableFrom(typeof(char?))
||fieldType.IsAssignableFrom(typeof(double?))
||fieldType.IsAssignableFrom(typeof(float?))
||fieldType.IsAssignableFrom(typeof(int?))
||fieldType.IsAssignableFrom(typeof(long?)))
{
trueValue = value;
}
else if (fieldType.IsAssignableFrom(typeof(String)))
{ {
if (value.GetType().IsAssignableFrom(typeof(DateTime))) object trueValue = null;
if (fieldType.IsAssignableFrom(typeof(bool?))
|| fieldType.IsAssignableFrom(typeof(byte?))
|| fieldType.IsAssignableFrom(typeof(char?))
|| fieldType.IsAssignableFrom(typeof(double?))
|| fieldType.IsAssignableFrom(typeof(float?))
|| fieldType.IsAssignableFrom(typeof(int?))
|| fieldType.IsAssignableFrom(typeof(long?)))
{
trueValue = value;
}
else if (fieldType.IsAssignableFrom(typeof(String)))
{
if (value.GetType().IsAssignableFrom(typeof(DateTime)))
{
DateTime dateTime = (DateTime)value;
trueValue = DateUtil.formatForOcean(dateTime);
}
else
{
trueValue = value;
}
}
else if (fieldType.IsAssignableFrom(typeof(DateTime?)))
{ {
DateTime dateTime = (DateTime)value; DateTime dateTime = (DateTime)value;
trueValue = DateUtil.formatForOcean(dateTime); trueValue = DateUtil.format(dateTime);
}
else if (fieldType.IsAssignableFrom(typeof(Byte[])) || fieldType.IsAssignableFrom(typeof(byte[])))
{
trueValue = Convert.ToBase64String((byte[])value);
} }
else else
{ {
trueValue = value; DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(fieldType);
MemoryStream stream = new MemoryStream();
dataContractJsonSerializer.WriteObject(stream, value);
byte[] dataBytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(dataBytes, 0, (int)stream.Length);
string dataString = Encoding.UTF8.GetString(dataBytes);
trueValue = dataString;
} }
result.Add(piName, trueValue);
} }
else if (fieldType.IsAssignableFrom(typeof(DateTime?)))
{
DateTime dateTime = (DateTime)value;
trueValue = DateUtil.format(dateTime);
}
else if (fieldType.IsAssignableFrom(typeof(Byte[])) || fieldType.IsAssignableFrom(typeof(byte[])))
{
trueValue = Convert.ToBase64String((byte[])value);
}
else
{
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(fieldType);
MemoryStream stream = new MemoryStream();
dataContractJsonSerializer.WriteObject(stream, value);
byte[] dataBytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(dataBytes, 0, (int)stream.Length);
string dataString = Encoding.UTF8.GetString(dataBytes);
trueValue = dataString;
}
result.Add(piName, trueValue);
} }
}
return result;
}
private Dictionary<string, object> serializeParam(object param)
{
Dictionary<string, object> result = new Dictionary<string, object>();
Type type = param.GetType();
var properties = type.GetProperties();
foreach (var property in properties)
{
var value = property.GetValue(param);
var valueType = value.GetType();
if (valueType.IsClass)
{
result.Add(property.Name, JsonConvert.SerializeObject(value));
}
else
{
result.Add(property.Name, value);
} }
} }
return result; return result;

Loading…
Cancel
Save