Skip to content

Commit

Permalink
initial load
Browse files Browse the repository at this point in the history
  • Loading branch information
trossr32 committed Dec 9, 2022
1 parent a20d1e4 commit 8967416
Show file tree
Hide file tree
Showing 181 changed files with 58,682 additions and 2 deletions.
64 changes: 62 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,62 @@
# ps-json-to-powershell-class
A Powershell module that creates Powershell classes and example usage functions from json supplied as a string, file or url.
# PsJsonToPowershellClass

[![PowerShell Gallery Version](https://img.shields.io/powershellgallery/v/ PsJsonToPowershellClass?label= PsJsonToPowershellClass&logo=powershell&style=plastic)](https://www.powershellgallery.com/packages/PsJsonToPowershellClass)
[![PowerShell Gallery](https://img.shields.io/powershellgallery/dt/PsJsonToPowershellClass?style=plastic)](https://www.powershellgallery.com/packages/PsJsonToPowershellClass)

A Powershell module that converts JSON to Powershell classes. JSON can be supplied as a string, a file that will be read or a URL that will be downloaded.

Available in the [Powershell Gallery](https://www.powershellgallery.com/packages/PsJsonToPowershellClass)

## Description
Convert JSON to Powershell classes. JSON can be supplied as a string, a file that will be read or a URL that will be downloaded.

Optionally create functions showing example usage.

Optionally write an output file.

Optionally copy the output to clipboard.

## Installation (from the Powershell Gallery)

```powershell
Install-Module PsJsonToPowershellClass
Import-Module PsJsonToPowershellClass
```

## Included cmdlets

```powershell
Convert-JsonToPowershellClass
```

## Examples

### Convert a JSON file to Powershell classes including usage examples and copy the output to clipboard.

```powershell
Convert-JsonToPowershellClass -JsonFile 'C:\Temp\a-json-file.json' -CopyToClipboard -IncludeExamples
```

## Building the module and importing locally

### Build the .NET core solution

```powershell
dotnet build [Github clone/download directory]\ps-json-to-powershell-class\src\PsJsonToPowershellClass\PsJsonToPowershellClass.csproj
```

### Copy the built files to your Powershell modules directory

Remove any existing installation in this directory, create a new module directory and copy all the built files.

```powershell
Remove-Item "C:\Users\[User]\Documents\PowerShell\Modules\PsJsonToPowershellClass" -Recurse -Force -ErrorAction SilentlyContinue
New-Item -Path 'C:\Users\[User]\Documents\PowerShell\Modules\PsJsonToPowershellClass' -ItemType Directory
Get-ChildItem -Path "[Github clone/download directory]\ps-json-to-powershell-class\src\PsJsonToPowershellClass\bin\Debug\net6.0\" | Copy-Item -Destination "C:\Users\[User]\Documents\PowerShell\Modules\PsJsonToPowershellClass" -Recurse
```

## Contribute

Please raise an issue if you find a bug or want to request a new feature, or create a pull request to contribute.

<a href='https://ko-fi.com/K3K22CEIT' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi4.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
Binary file added assets/images/ps7_icon_256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/ps7_icon_88.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions src/JsonToPowershellClass.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Net;
using Flurl.Http;
using JsonToPowershellClass.Core.Enums;
using JsonToPowershellClass.Core.Models;
using JsonToPowershellClass.Core.Services;
using Microsoft.AspNetCore.Mvc;
using JsonToPowershellClass.Web.ViewModels;

namespace JsonToPowershellClass.Web.Controllers;

public class HomeController : Controller
{
private readonly IJsonClassGeneratorService _jsonClassGeneratorSvc;

public HomeController(IJsonClassGeneratorService jsonClassGeneratorSvc)
{
_jsonClassGeneratorSvc = jsonClassGeneratorSvc;
}

[HttpGet]
public async Task<IActionResult> Index() => View(new IndexViewModel());

[HttpPost]
public async Task<IActionResult> Index(IndexViewModel model)
{
if (string.IsNullOrWhiteSpace(model.ClassName))
{
model.ClassName = "RootObject";
}
else if (string.IsNullOrWhiteSpace(model.Json))
{
model.Error = true;
model.ErrorNo = 2;
}

//todo - add description to index to say url can be used or create separate URL text box
if (model.Json?.ToLower().StartsWith("http") ?? false)
{
try
{
model.Json = await model.Json.GetStringAsync();
}
catch (Exception)
{
model.Error = true;
model.ErrorNo = 4;
}
}

if (model.Error)
return View(model);

try
{
model.CodeObjects = WebUtility.HtmlEncode(Prepare(model.Json, model.ClassName, model.Pascal, model.AddExampleFunctions));
}
catch (Exception)
{
model.Error = true;
model.ErrorNo = 3;
}

return View(model);
}

private string Prepare(string json, string classname, bool pascal, bool addExampleFunctions) =>
string.IsNullOrWhiteSpace(json)
? null
: _jsonClassGeneratorSvc.GenerateClasses(json, new JsonSourceWrapper{Source = InputSource.FromString}, classname, pascal, addExampleFunctions);

//private string Result { get; set; }

//public void R(object d)
//{
// foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(d))
// {
// string name = descriptor.Name;
// object value = descriptor.GetValue(d);
// Result += $"{name}={value}";

// if (value is JToken {Type: JTokenType.Object} token)
// R(token);
// }
//}
}
172 changes: 172 additions & 0 deletions src/JsonToPowershellClass.Web/JsonToPowershellClass.Web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<None Include="wwwroot\Content\bootstrap-theme.css" />
<None Include="wwwroot\Content\bootstrap-theme.css.map" />
<None Include="wwwroot\Content\bootstrap-theme.min.css" />
<None Include="wwwroot\Content\bootstrap.css" />
<None Include="wwwroot\Content\bootstrap.css.map" />
<None Include="wwwroot\Content\bootstrap.min.css" />
<None Include="wwwroot\Content\Images\copy-32px-color.png" />
<None Include="wwwroot\Content\Images\copy-32px.png" />
<None Include="wwwroot\Content\JsonEditor\docs\api.md" />
<None Include="wwwroot\Content\JsonEditor\docs\usage.md" />
<None Include="wwwroot\Content\JsonEditor\examples\01_basic_usage.html" />
<None Include="wwwroot\Content\JsonEditor\examples\02_viewer.html" />
<None Include="wwwroot\Content\JsonEditor\examples\03_switch_mode.html" />
<None Include="wwwroot\Content\JsonEditor\examples\requirejs_demo\requirejs_demo.html" />
<None Include="wwwroot\Content\JsonEditor\examples\requirejs_demo\scripts\main.js" />
<None Include="wwwroot\Content\JsonEditor\examples\requirejs_demo\scripts\require.js" />
<None Include="wwwroot\Content\JsonEditor\HISTORY.md" />
<None Include="wwwroot\Content\JsonEditor\img\jsoneditor-icons.png" />
<None Include="wwwroot\Content\JsonEditor\jsoneditor-min.css" />
<None Include="wwwroot\Content\JsonEditor\jsoneditor-min.js" />
<None Include="wwwroot\Content\JsonEditor\jsoneditor.css" />
<None Include="wwwroot\Content\JsonEditor\jsoneditor.js" />
<None Include="wwwroot\Content\JsonEditor\lib\ace\ace.js" />
<None Include="wwwroot\Content\JsonEditor\lib\ace\ext-searchbox.js" />
<None Include="wwwroot\Content\JsonEditor\lib\ace\mode-json.js" />
<None Include="wwwroot\Content\JsonEditor\lib\ace\theme-jsoneditor.js" />
<None Include="wwwroot\Content\JsonEditor\lib\ace\theme-textmate.js" />
<None Include="wwwroot\Content\JsonEditor\lib\ace\worker-json.js" />
<None Include="wwwroot\Content\JsonEditor\lib\jsonlint\jsonlint.js" />
<None Include="wwwroot\Content\JsonEditor\lib\jsonlint\README.md" />
<None Include="wwwroot\Content\JsonEditor\LICENSE" />
<None Include="wwwroot\Content\JsonEditor\NOTICE" />
<None Include="wwwroot\Content\JsonEditor\README.md" />
<None Include="wwwroot\Content\prettify.css" />
<None Include="wwwroot\Content\Site.css" />
<None Include="wwwroot\Content\SunLight\CHANGELOG" />
<None Include="wwwroot\Content\SunLight\COPYING" />
<None Include="wwwroot\Content\SunLight\jquery.sunlight.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.6502asm-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.actionscript-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.bash-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.batch-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.brainfuck-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.cpp-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.csharp-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.css-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.diff-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.erlang-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.haskell-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.httpd-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.java-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.javascript-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.lisp-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.lua-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.mysql-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.nginx-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.objective-c-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.perl-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.php-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.powershell-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.python-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.ruby-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.scala-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.sln-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.tsql-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.vb-min.js" />
<None Include="wwwroot\Content\SunLight\lang\sunlight.xml-min.js" />
<None Include="wwwroot\Content\SunLight\plugins\sunlight-plugin.doclinks-min.js" />
<None Include="wwwroot\Content\SunLight\plugins\sunlight-plugin.linenumbers-min.js" />
<None Include="wwwroot\Content\SunLight\plugins\sunlight-plugin.menu-min.js" />
<None Include="wwwroot\Content\SunLight\README.md" />
<None Include="wwwroot\Content\SunLight\sunlight-all-min.js" />
<None Include="wwwroot\Content\SunLight\sunlight-min.js" />
<None Include="wwwroot\Content\SunLight\themes\sunlight.dark.css" />
<None Include="wwwroot\Content\SunLight\themes\sunlight.default.css" />
<None Include="wwwroot\Content\themes\base\images\ui-bg_flat_0_aaaaaa_40x100.png" />
<None Include="wwwroot\Content\themes\base\images\ui-bg_flat_75_ffffff_40x100.png" />
<None Include="wwwroot\Content\themes\base\images\ui-bg_glass_55_fbf9ee_1x400.png" />
<None Include="wwwroot\Content\themes\base\images\ui-bg_glass_65_ffffff_1x400.png" />
<None Include="wwwroot\Content\themes\base\images\ui-bg_glass_75_dadada_1x400.png" />
<None Include="wwwroot\Content\themes\base\images\ui-bg_glass_75_e6e6e6_1x400.png" />
<None Include="wwwroot\Content\themes\base\images\ui-bg_glass_95_fef1ec_1x400.png" />
<None Include="wwwroot\Content\themes\base\images\ui-bg_highlight-soft_75_cccccc_1x100.png" />
<None Include="wwwroot\Content\themes\base\images\ui-icons_222222_256x240.png" />
<None Include="wwwroot\Content\themes\base\images\ui-icons_2e83ff_256x240.png" />
<None Include="wwwroot\Content\themes\base\images\ui-icons_454545_256x240.png" />
<None Include="wwwroot\Content\themes\base\images\ui-icons_888888_256x240.png" />
<None Include="wwwroot\Content\themes\base\images\ui-icons_cd0a0a_256x240.png" />
<None Include="wwwroot\Content\themes\base\jquery-ui.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.accordion.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.all.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.autocomplete.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.base.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.button.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.core.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.datepicker.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.dialog.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.progressbar.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.resizable.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.selectable.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.slider.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.tabs.css" />
<None Include="wwwroot\Content\themes\base\jquery.ui.theme.css" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-bg_flat_0_aaaaaa_40x100.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-bg_flat_75_ffffff_40x100.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-bg_glass_55_fbf9ee_1x400.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-bg_glass_65_ffffff_1x400.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-bg_glass_75_dadada_1x400.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-bg_glass_75_e6e6e6_1x400.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-bg_glass_95_fef1ec_1x400.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-bg_highlight-soft_75_cccccc_1x100.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-icons_222222_256x240.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-icons_2e83ff_256x240.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-icons_454545_256x240.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-icons_888888_256x240.png" />
<None Include="wwwroot\Content\themes\base\minified\images\ui-icons_cd0a0a_256x240.png" />
<None Include="wwwroot\Content\themes\base\minified\jquery-ui.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.accordion.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.autocomplete.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.button.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.core.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.datepicker.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.dialog.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.progressbar.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.resizable.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.selectable.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.slider.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.tabs.min.css" />
<None Include="wwwroot\Content\themes\base\minified\jquery.ui.theme.min.css" />
<None Include="wwwroot\fonts\glyphicons-halflings-regular.eot" />
<None Include="wwwroot\fonts\glyphicons-halflings-regular.svg" />
<None Include="wwwroot\fonts\glyphicons-halflings-regular.ttf" />
<None Include="wwwroot\fonts\glyphicons-halflings-regular.woff" />
<None Include="wwwroot\Scripts\bootstrap.js" />
<None Include="wwwroot\Scripts\bootstrap.min.js" />
<None Include="wwwroot\Scripts\jquery-1.9.1.intellisense.js" />
<None Include="wwwroot\Scripts\jquery-1.9.1.js" />
<None Include="wwwroot\Scripts\jquery-1.9.1.min.js" />
<None Include="wwwroot\Scripts\jquery-1.9.1.min.map" />
<None Include="wwwroot\Scripts\jquery-ui-1.8.24.js" />
<None Include="wwwroot\Scripts\jquery-ui-1.8.24.min.js" />
<None Include="wwwroot\Scripts\jquery.unobtrusive-ajax.js" />
<None Include="wwwroot\Scripts\jquery.unobtrusive-ajax.min.js" />
<None Include="wwwroot\Scripts\jquery.validate-vsdoc.js" />
<None Include="wwwroot\Scripts\jquery.validate.js" />
<None Include="wwwroot\Scripts\jquery.validate.min.js" />
<None Include="wwwroot\Scripts\jquery.validate.unobtrusive.js" />
<None Include="wwwroot\Scripts\jquery.validate.unobtrusive.min.js" />
<None Include="wwwroot\Scripts\knockout-2.2.0.debug.js" />
<None Include="wwwroot\Scripts\knockout-2.2.0.js" />
<None Include="wwwroot\Scripts\modernizr-2.6.2.js" />
<None Include="wwwroot\Scripts\_references.js" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Flurl.Http" Version="3.2.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\JsonToPowershellClass\JsonToPowershellClass.Core.csproj" />
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions src/JsonToPowershellClass.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using JsonToPowershellClass.Core.Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

builder.Services.AddSession();
builder.Services.AddMemoryCache();

builder.Services.AddScoped<IJsonClassGeneratorService, JsonClassGeneratorService>();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");

app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseSession();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});

app.MapRazorPages();

app.Run();
28 changes: 28 additions & 0 deletions src/JsonToPowershellClass.Web/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:40676",
"sslPort": 44363
}
},
"profiles": {
"JsonToPowershellClass.Web": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7141;http://localhost:5259",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading

0 comments on commit 8967416

Please sign in to comment.