Skip to content

Commit

Permalink
Code refactor, remove newtonsoft.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Seji64 committed Jul 10, 2024
1 parent afad4a8 commit cceedf2
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 137 deletions.
4 changes: 2 additions & 2 deletions Classes/ConvertOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace GPXRide.Classes
public class ConvertOptions
{
public bool Motorway { get; set; } = true;
public bool TollFree { get; set; } = false;
public bool DirtyRoads { get; set; } = false;
public bool TollFree { get; set; }
public bool DirtyRoads { get; set; }
public bool Tunnel { get; set; } = true;
public bool Trains { get; set; } = true;
public bool Ferry { get; set; } = true;
Expand Down
10 changes: 4 additions & 6 deletions Classes/ConvertTask.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using Geo.Gps.Serialization.Xml.Gpx.Gpx11;
using GPXRide.Enums;
using MudBlazor;

namespace GPXRide.Classes
{
public class ConvertTask
{
public int? Id { get; set; } = null;
public GpxFile OriginalGpxFile { get; set; } = null;
public string FileName { get; set; }
public ItineraryFile ConvertedItineraryFile { get; set; } = null;
public int? Id { get; init; }
public GpxFile OriginalGpxFile { get; set; }
public string FileName { get; init; }
public ItineraryFile ConvertedItineraryFile { get; set; }
public ConvertState State { get; set; } = ConvertState.None;
public ConvertOptions ConvertOptions { get; set; } = new();
public MudChip SelectedSourceChip { get; set; }
public SourceType SourceType { get; set; }
}
}
35 changes: 35 additions & 0 deletions Classes/GpsRow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Text.Json.Serialization;

namespace GPXRide.Classes;
public class GpsRow
{
[JsonPropertyName("rollAngle")]
public double RollAngle { get; set; }
[JsonPropertyName("gear")]
public int Gear { get; set; }
[JsonPropertyName("index")]
public int Index { get; set; }
[JsonPropertyName("engineSpeed")]
public int EngineSpeed { get; set; }
[JsonPropertyName("tripId")]
public string TripId { get; set; }
[JsonPropertyName("throttle")]
public int Throttle { get; set; }
[JsonPropertyName("fix")]
public int Fix { get; set; }
[JsonPropertyName("altitude")]
public double Altitude { get; set; }
[JsonPropertyName("time")]
public string Time { get; set; }
[JsonPropertyName("airTemperature")]
public int AirTemperature { get; set; }
[JsonPropertyName("latitude")]
public double Latitude { get; set; }
[JsonPropertyName("distanceInMeterFromStart")]
public int DistanceInMeterFromStart { get; set; }
[JsonPropertyName("rpm")]
public int Rpm { get; set; }
[JsonPropertyName("longitude")]
public double Longitude { get; set; }
}

10 changes: 5 additions & 5 deletions Classes/Gpx11SerializerAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class Gpx11SerializerAsync : Gpx11Serializer
{
public static async Task<GpxFile> DeserializeAsync(Stream stream)
{
using var streamReader = new StreamReader(stream);
var data = await streamReader.ReadToEndAsync();
var xmlSerializer = new XmlSerializer(typeof(GpxFile));
using StreamReader streamReader = new StreamReader(stream);
string data = await streamReader.ReadToEndAsync();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(GpxFile));

using var stringReader = new StringReader(data);
var gpxFile = (GpxFile)xmlSerializer.Deserialize(stringReader);
using StringReader stringReader = new StringReader(data);
GpxFile gpxFile = (GpxFile)xmlSerializer.Deserialize(stringReader);
return gpxFile;
}
}
Expand Down
20 changes: 10 additions & 10 deletions Classes/Itinerary.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace GPXRide.Classes
{
public class Itinerary
{
[JsonProperty("length")]
[JsonPropertyName("length")]
public int Length { get; set; }

[JsonProperty("preferences")]
[JsonPropertyName("preferences")]
public Preferences Preferences { get; set; } = new();

[JsonProperty("id")]
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonProperty("creationDate")]
[JsonPropertyName("creationDate")]
public DateTime CreationDate { get; set; } = DateTime.Now;

[JsonProperty("nations")]
[JsonPropertyName("nations")]
public List<object> Nations { get; set; } = [];

[JsonProperty("duration")]
[JsonPropertyName("duration")]
public int Duration { get; set; }

[JsonProperty("stops")]
[JsonPropertyName("stops")]
public List<Stop> Stops { get; set; } = [];

[JsonProperty("name")]
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonProperty("vehicleClass")]
[JsonPropertyName("vehicleClass")]
public string VehicleClass { get; set; }
}
}
16 changes: 8 additions & 8 deletions Classes/ItineraryFile.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using System.IO;
using System.IO.Compression;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text;
using System.Text.Json;

namespace GPXRide.Classes
{
public class ItineraryFile
{
public Itinerary Itinerary { get; set; } = new();
public Itinerary Itinerary { get; } = new();
public MemoryStream ToZipArchiveStream()
{
MemoryStream memoryStream = new();

using var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true);
var demoFile = archive.CreateEntry("itinerary.json");
using ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true);
ZipArchiveEntry demoFile = archive.CreateEntry("itinerary.json");

using var entryStream = demoFile.Open();
var ItineraryText = JsonConvert.SerializeObject(Itinerary, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-ddTHH:mm:ssZ" });
MemoryStream stream = new(System.Text.Encoding.UTF8.GetBytes(ItineraryText));
using Stream entryStream = demoFile.Open();
string itineraryText = JsonSerializer.Serialize(Itinerary, new JsonSerializerOptions() { Converters = { new JsonDateTimeConverter() }});
MemoryStream stream = new(Encoding.UTF8.GetBytes(itineraryText));
stream.WriteTo(entryStream);

return memoryStream;
Expand Down
20 changes: 20 additions & 0 deletions Classes/JsonDateTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace GPXRide.Classes;

public class JsonDateTimeConverter : JsonConverter<DateTime>
{
private const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ssZ";

public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.ParseExact(reader.GetString() ?? string.Empty, DateTimeFormat, null);
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(DateTimeFormat));
}
}
14 changes: 7 additions & 7 deletions Classes/Preferences.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace GPXRide.Classes
{
public class Preferences
{
[JsonProperty("motorway")]
[JsonPropertyName("motorway")]
public bool Motorway { get; set; }

[JsonProperty("tunnel")]
[JsonPropertyName("tunnel")]
public bool Tunnel { get; set; }

[JsonProperty("dirtRoads")]
[JsonPropertyName("dirtRoads")]
public bool DirtRoads { get; set; }

[JsonProperty("trains")]
[JsonPropertyName("trains")]
public bool Trains { get; set; }

[JsonProperty("tollFree")]
[JsonPropertyName("tollFree")]
public bool TollFree { get; set; }

[JsonProperty("ferry")]
[JsonPropertyName("ferry")]
public bool Ferry { get; set; }
}
}
14 changes: 7 additions & 7 deletions Classes/Stop.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
using System;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace GPXRide.Classes
{
public class Stop
{
[JsonProperty("date")]
[JsonPropertyName("date")]
public DateTime Date { get; set; } = DateTime.Now;

[JsonProperty("isMyPosition")]
[JsonPropertyName("isMyPosition")]
public bool IsMyPosition { get; set; }

[JsonProperty("latitude")]
[JsonPropertyName("latitude")]
public decimal Latitude { get; set; }

[JsonProperty("city")]
[JsonPropertyName("city")]
public string City { get; set; }

[JsonProperty("longitude")]
[JsonPropertyName("longitude")]
public decimal Longitude { get; set; }

[JsonProperty("address")]
[JsonPropertyName("address")]
public string Address { get; set; }
}
}
56 changes: 56 additions & 0 deletions Classes/Trip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace GPXRide.Classes;

public class Trip
{
[JsonPropertyName(nameof(Id))]
public string Id { get; set; }
[JsonPropertyName("bikeModel")]
public string BikeModel { get; set; }
[JsonPropertyName("totalTimeInSeconds")]
public int TotalTimeInSeconds { get; set; }
[JsonPropertyName("downloadedSizeInBytes")]
public int DownloadedSizeInBytes { get; set; }
[JsonPropertyName("maxThrottle")]
public int MaxThrottle { get; set; }
[JsonPropertyName("isCompleted")]
public bool IsCompleted { get; set; }
[JsonPropertyName("startAddress")]
public int StartAddress { get; set; }
[JsonPropertyName("endDate")]
public string EndDate { get; set; }
[JsonPropertyName("skippedRowsForMissingData")]
public int SkippedRowsForMissingData { get; set; }
[JsonPropertyName("countries")]
public string[] Countries { get; set; }
[JsonPropertyName("maxRollAngle")]
public double MaxRollAngle { get; set; }
[JsonPropertyName("uom")]
public string Uom { get; set; }
[JsonPropertyName("title")]
public string Title { get; set; }
[JsonPropertyName("stopAddress")]
public int StopAddress { get; set; }
[JsonPropertyName("startDate")]
public string StartDate { get; set; }
[JsonPropertyName("maxSpeedKmh")]
public int MaxSpeedKmh { get; set; }
[JsonPropertyName("imageName")]
public string ImageName { get; set; }
[JsonPropertyName("totalDistanceInMeters")]
public int TotalDistanceInMeters { get; set; }
[JsonPropertyName("temporaryGpsUnitTripId")]
public int TemporaryGpsUnitTripId { get; set; }
[JsonPropertyName("skippedRowsForInvalidLocation")]
public int SkippedRowsForInvalidLocation { get; set; }
[JsonPropertyName("avgSpeedKmh")]
public double AvgSpeedKmh { get; set; }
[JsonPropertyName("language")]
public string Language { get; set; }

[JsonIgnore]
public List<GpsRow> GpsRows { get; set; }

}
9 changes: 4 additions & 5 deletions GPXRide.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
<ItemGroup>
<PackageReference Include="BlazorDownloadFile" Version="2.4.0.2" />
<PackageReference Include="Geo" Version="1.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.3" PrivateAssets="all" />
<PackageReference Include="MudBlazor" Version="6.19.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="PublishSPAforGitHubPages.Build" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.7" PrivateAssets="all" />
<PackageReference Include="MudBlazor" Version="7.0.0" />
<PackageReference Include="PublishSPAforGitHubPages.Build" Version="2.2.0" />
<PackageReference Include="Serilog.Sinks.BrowserConsole" Version="2.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>
Expand Down
Loading

0 comments on commit cceedf2

Please sign in to comment.