Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit c30b240

Browse files
Serializing dimension values as strings
1 parent ce682ce commit c30b240

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/GitHub.Exports/Services/MetricsService.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,40 @@ public Task SendOptIn()
102102
static StringContent SerializeRequest(UsageModel model)
103103
{
104104
var serializer = new SimpleJsonSerializer();
105-
var dictionary = ToModelDictionary(model);
105+
var dictionary = new Dictionary<string, object>
106+
{
107+
{ToJsonPropertyName("Dimensions"), ToStringDictionary(model.Dimensions) },
108+
{ToJsonPropertyName("Measures"), ToObjectDictionary(model.Measures) }
109+
};
106110
return new StringContent(serializer.Serialize(dictionary), Encoding.UTF8, "application/json");
107111
}
108112

109-
static Dictionary<string, object> ToModelDictionary(object model)
113+
static Dictionary<string, string> ToStringDictionary(object model)
110114
{
111-
var dict = new Dictionary<string, object>();
115+
var dict = new Dictionary<string, string>();
112116
var type = model.GetType();
113117

114118
foreach (var prop in type.GetProperties())
115119
{
116120
if (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string))
117121
{
118-
dict.Add(ToJsonPropertyName(prop.Name), prop.GetValue(model));
122+
dict.Add(ToJsonPropertyName(prop.Name), prop.GetValue(model).ToString());
119123
}
120-
else
124+
}
125+
126+
return dict;
127+
}
128+
129+
static Dictionary<string, object> ToObjectDictionary(object model)
130+
{
131+
var dict = new Dictionary<string, object>();
132+
var type = model.GetType();
133+
134+
foreach (var prop in type.GetProperties())
135+
{
136+
if (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string))
121137
{
122-
var value = prop.GetValue(model);
123-
124-
if (value == null)
125-
{
126-
dict.Add(ToJsonPropertyName(prop.Name), value);
127-
}
128-
else
129-
{
130-
dict.Add(ToJsonPropertyName(prop.Name), ToModelDictionary(value));
131-
}
138+
dict.Add(ToJsonPropertyName(prop.Name), prop.GetValue(model));
132139
}
133140
}
134141

0 commit comments

Comments
 (0)