Skip to content

Commit 66ff319

Browse files
Merge pull request #178 from contentstack/enhc/DX-7298
feat: migrate Environment and Global Field modules to System.Text.Json
2 parents 1531fa2 + 83c9b6b commit 66ff319

8 files changed

Lines changed: 46 additions & 39 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [v1.0.0-beta.5](https://github.com/contentstack/contentstack-management-dotnet/tree/v1.0.0-beta.5)
4+
- **Environment & Global Field STJ Migration**
5+
- Migrated Environment and Global Field modules from Newtonsoft.Json to System.Text.Json
6+
- Updated service constructors to use JsonSerializerOptions and Utf8JsonWriter serialization
7+
- Re-enabled Environment() and GlobalField() methods in Stack class
8+
39
## [v1.0.0-beta.4](https://github.com/contentstack/contentstack-management-dotnet/tree/v1.0.0-beta.4)
410
- **Assets STJ Migration Complete**
511
- Fully migrated Assets module from Newtonsoft.Json to System.Text.Json
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
using System.Collections.Generic;
2-
using Newtonsoft.Json;
2+
using System.Text.Json.Serialization;
3+
34
namespace Contentstack.Management.Core.Models
45
{
5-
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
66
public class EnvironmentModel
77
{
8-
[JsonProperty(propertyName: "name")]
8+
[JsonPropertyName("name")]
99
public string Name { get; set; }
10-
[JsonProperty(propertyName: "servers")]
10+
11+
[JsonPropertyName("servers")]
1112
public List<Server> Servers { get; set; }
12-
[JsonProperty(propertyName: "urls")]
13+
14+
[JsonPropertyName("urls")]
1315
public List<LocalesUrl> Urls { get; set; }
14-
[JsonProperty(propertyName: "deploy_content")]
15-
public bool DeployContent { get; set; } = true;
1616

17+
[JsonPropertyName("deploy_content")]
18+
public bool DeployContent { get; set; } = true;
1719
}
1820

1921
public class Server
2022
{
21-
[JsonProperty(propertyName: "name")]
23+
[JsonPropertyName("name")]
2224
public string Name { get; set; }
2325
}
2426

2527
public class LocalesUrl
2628
{
27-
[JsonProperty(propertyName: "url")]
29+
[JsonPropertyName("url")]
2830
public string Url { get; set; }
29-
[JsonProperty(propertyName: "locale")]
31+
32+
[JsonPropertyName("locale")]
3033
public string Locale { get; set; }
3134
}
3235
}

Contentstack.Management.Core/Models/GlobalField.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
22
using Contentstack.Management.Core.Queryable;
33
using Contentstack.Management.Core.Services.Models;
44

@@ -46,7 +46,7 @@ public Query Query()
4646
public override ContentstackResponse Create(ContentModelling model, ParameterCollection collection = null)
4747
{
4848
ThrowIfUidNotEmpty();
49-
var service = new GlobalFieldService(stack.client.serializer, stack, resourcePath, model, this.fieldName, apiVersion, collection: collection);
49+
var service = new GlobalFieldService(stack.client.SerializerOptions, stack, resourcePath, model, this.fieldName, apiVersion, collection: collection);
5050
return stack.client.InvokeSync(service, apiVersion: apiVersion);
5151
}
5252

@@ -66,7 +66,7 @@ public override Task<ContentstackResponse> CreateAsync(ContentModelling model, P
6666
{
6767
ThrowIfUidNotEmpty();
6868
stack.ThrowIfNotLoggedIn();
69-
var service = new GlobalFieldService(stack.client.serializer, stack, resourcePath, model, this.fieldName, apiVersion, collection: collection);
69+
var service = new GlobalFieldService(stack.client.SerializerOptions, stack, resourcePath, model, this.fieldName, apiVersion, collection: collection);
7070
return stack.client.InvokeAsync<GlobalFieldService, ContentstackResponse>(service, apiVersion: apiVersion);
7171
}
7272

@@ -85,7 +85,7 @@ public override Task<ContentstackResponse> CreateAsync(ContentModelling model, P
8585
public override ContentstackResponse Update(ContentModelling model, ParameterCollection collection = null)
8686
{
8787
ThrowIfUidEmpty();
88-
var service = new GlobalFieldService(stack.client.serializer, stack, resourcePath, model, this.fieldName, apiVersion, "PUT", collection: collection);
88+
var service = new GlobalFieldService(stack.client.SerializerOptions, stack, resourcePath, model, this.fieldName, apiVersion, "PUT", collection: collection);
8989
return stack.client.InvokeSync(service, apiVersion: apiVersion);
9090
}
9191

@@ -105,7 +105,7 @@ public override Task<ContentstackResponse> UpdateAsync(ContentModelling model, P
105105
{
106106
stack.ThrowIfNotLoggedIn();
107107
ThrowIfUidEmpty();
108-
var service = new GlobalFieldService(stack.client.serializer, stack, resourcePath, model, this.fieldName, apiVersion, "PUT", collection: collection);
108+
var service = new GlobalFieldService(stack.client.SerializerOptions, stack, resourcePath, model, this.fieldName, apiVersion, "PUT", collection: collection);
109109
return stack.client.InvokeAsync<GlobalFieldService, ContentstackResponse>(service, apiVersion: apiVersion);
110110
}
111111

@@ -123,7 +123,7 @@ public override ContentstackResponse Fetch(ParameterCollection collection = null
123123
{
124124
stack.ThrowIfNotLoggedIn();
125125
ThrowIfUidEmpty();
126-
var service = new GlobalFieldFetchDeleteService(stack.client.serializer, stack, resourcePath, apiVersion, collection: collection);
126+
var service = new GlobalFieldFetchDeleteService(stack.client.SerializerOptions, stack, resourcePath, apiVersion, collection: collection);
127127
return stack.client.InvokeSync(service, apiVersion: apiVersion);
128128
}
129129

@@ -141,7 +141,7 @@ public override Task<ContentstackResponse> FetchAsync(ParameterCollection collec
141141
{
142142
stack.ThrowIfNotLoggedIn();
143143
ThrowIfUidEmpty();
144-
var service = new GlobalFieldFetchDeleteService(stack.client.serializer, stack, resourcePath, apiVersion, collection: collection);
144+
var service = new GlobalFieldFetchDeleteService(stack.client.SerializerOptions, stack, resourcePath, apiVersion, collection: collection);
145145
return stack.client.InvokeAsync<GlobalFieldFetchDeleteService, ContentstackResponse>(service, apiVersion: apiVersion);
146146
}
147147

@@ -159,7 +159,7 @@ public override ContentstackResponse Delete(ParameterCollection collection = nul
159159
{
160160
stack.ThrowIfNotLoggedIn();
161161
ThrowIfUidEmpty();
162-
var service = new GlobalFieldFetchDeleteService(stack.client.serializer, stack, resourcePath, apiVersion, "DELETE", collection: collection);
162+
var service = new GlobalFieldFetchDeleteService(stack.client.SerializerOptions, stack, resourcePath, apiVersion, "DELETE", collection: collection);
163163
return stack.client.InvokeSync(service, apiVersion: apiVersion);
164164
}
165165

@@ -177,7 +177,7 @@ public override Task<ContentstackResponse> DeleteAsync(ParameterCollection colle
177177
{
178178
stack.ThrowIfNotLoggedIn();
179179
ThrowIfUidEmpty();
180-
var service = new GlobalFieldFetchDeleteService(stack.client.serializer, stack, resourcePath, apiVersion, "DELETE", collection: collection);
180+
var service = new GlobalFieldFetchDeleteService(stack.client.SerializerOptions, stack, resourcePath, apiVersion, "DELETE", collection: collection);
181181
return stack.client.InvokeAsync<GlobalFieldFetchDeleteService, ContentstackResponse>(service, apiVersion: apiVersion);
182182
}
183183
}

Contentstack.Management.Core/Models/Stack.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,6 @@ public Asset Asset(string uid = null)
645645
return new Asset(this, uid);
646646
}
647647

648-
/*
649648
/// <summary>
650649
/// <see cref="Models.GlobalField" /> defines the structure or schema of a page or a section of your web or mobile property. To create global Fields for your application, you are required to first create a global field. Read more about <a href='https://www.contentstack.com/docs/guide/global-fields'>Global Fields</a>.
651650
/// </summary>
@@ -727,6 +726,7 @@ public Taxonomy Taxonomy(string uid = null)
727726
728727
return new Taxonomy(this, uid);
729728
}
729+
*/
730730

731731
/// <summary>
732732
/// A publishing <see cref="Models.Environment" /> corresponds to one or more deployment servers or a content delivery destination where the entries need to be published.
@@ -748,6 +748,7 @@ public Environment Environment(string uid = null)
748748
return new Environment(this, uid);
749749
}
750750

751+
/*
751752
/// <summary>
752753
/// You can use <see cref="Models.Token.DeliveryToken" /> to authenticate Content Delivery API (CDA) requests and retrieve the published content of an environment.
753754
/// </summary>

Contentstack.Management.Core/Services/Models/GlobalFieldFetchDeleteService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2+
using System.Text.Json;
23
using Contentstack.Management.Core.Queryable;
3-
using Newtonsoft.Json;
44
using Contentstack.Management.Core.Utils;
55

66
namespace Contentstack.Management.Core.Services.Models
@@ -15,8 +15,8 @@ internal class GlobalFieldFetchDeleteService : ContentstackService
1515
/// <summary>
1616
/// Initializes a new instance of the <see cref="GlobalFieldFetchDeleteService"/> class.
1717
/// </summary>
18-
internal GlobalFieldFetchDeleteService(JsonSerializer serializer, Core.Models.Stack stack, string resourcePath, string apiVersion, string httpMethod = "GET", ParameterCollection collection = null)
19-
: base(serializer, stack: stack, collection)
18+
internal GlobalFieldFetchDeleteService(JsonSerializerOptions serializerOptions, Core.Models.Stack stack, string resourcePath, string apiVersion, string httpMethod = "GET", ParameterCollection collection = null)
19+
: base(serializerOptions ?? stack?.client?.SerializerOptions ?? new JsonSerializerOptions(), stack: stack, collection)
2020
{
2121
if (stack.APIKey == null)
2222
{

Contentstack.Management.Core/Services/Models/GlobalFieldService.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System;
2-
using System.Globalization;
3-
using System.IO;
2+
using System.Collections.Generic;
3+
using System.Text.Json;
44
using Contentstack.Management.Core.Models;
55
using Contentstack.Management.Core.Queryable;
6-
using Newtonsoft.Json;
76
using Contentstack.Management.Core.Utils;
87

98
namespace Contentstack.Management.Core.Services.Models
@@ -20,8 +19,8 @@ internal class GlobalFieldService : ContentstackService
2019
/// <summary>
2120
/// Initializes a new instance of the <see cref="GlobalFieldService"/> class.
2221
/// </summary>
23-
internal GlobalFieldService(JsonSerializer serializer, Core.Models.Stack stack, string resourcePath, ContentModelling dataModel, string fieldName, string apiVersion, string httpMethod = "POST", ParameterCollection collection = null)
24-
: base(serializer, stack: stack, collection)
22+
internal GlobalFieldService(JsonSerializerOptions serializerOptions, Core.Models.Stack stack, string resourcePath, ContentModelling dataModel, string fieldName, string apiVersion, string httpMethod = "POST", ParameterCollection collection = null)
23+
: base(serializerOptions ?? stack?.client?.SerializerOptions ?? new JsonSerializerOptions(), stack: stack, collection)
2524
{
2625
if (stack.APIKey == null)
2726
{
@@ -60,14 +59,13 @@ internal GlobalFieldService(JsonSerializer serializer, Core.Models.Stack stack,
6059

6160
public override void ContentBody()
6261
{
63-
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
62+
var requestData = new Dictionary<string, object>
6463
{
65-
JsonWriter writer = new JsonTextWriter(stringWriter);
64+
{ fieldName, _typedModel }
65+
};
6666

67-
Serializer.Serialize(writer, _typedModel);
68-
string snippet = $"{{\"{fieldName}\": {stringWriter.ToString()}}}";
69-
this.ByteContent = System.Text.Encoding.UTF8.GetBytes(snippet);
70-
}
67+
string jsonString = JsonSerializer.Serialize(requestData, SerializerOptions);
68+
this.ByteContent = System.Text.Encoding.UTF8.GetBytes(jsonString);
7169
}
7270

7371
/// <summary>

Contentstack.Management.Core/contentstack.management.core.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@
8989
<!-- ContentType.cs - RE-ENABLED -->
9090
<Compile Remove="Models\Entry.cs" />
9191
<Compile Remove="Models\EntryVariant.cs" />
92-
<Compile Remove="Models\Environment.cs" />
92+
<!-- Environment.cs - RE-ENABLED for Environment module STJ migration -->
9393
<Compile Remove="Models\Extension.cs" />
9494
<!-- Folder.cs - RE-ENABLED for Assets module -->
95-
<Compile Remove="Models\GlobalField.cs" />
95+
<!-- GlobalField.cs - RE-ENABLED for Global Field module STJ migration -->
9696
<Compile Remove="Models\Label.cs" />
9797
<Compile Remove="Models\Locale.cs" />
9898
<!-- Organization.cs - RE-ENABLED -->
@@ -125,8 +125,7 @@
125125
<Compile Remove="Services\DeleteReleaseItemService.cs" />
126126
<!-- QueryService.cs - RE-ENABLED for ContentType listing -->
127127
<!-- Re-enable essential model services for ContentType -->
128-
<Compile Remove="Services\Models\GlobalFieldService.cs" />
129-
<Compile Remove="Services\Models\GlobalFieldFetchDeleteService.cs" />
128+
<!-- GlobalFieldService.cs, GlobalFieldFetchDeleteService.cs - RE-ENABLED for Global Field module STJ migration -->
130129
<Compile Remove="Services\Models\ImportExportService.cs" />
131130
<Compile Remove="Services\Models\LocaleService.cs" />
132131
<Compile Remove="Services\Models\LocalizationService.cs" />

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.0.0-beta.4</Version>
3+
<Version>1.0.0-beta.5</Version>
44
</PropertyGroup>
55
</Project>

0 commit comments

Comments
 (0)