hermanussen / jsonbyexamplegenerator Goto Github PK
View Code? Open in Web Editor NEWGenerate classes based on example json files in your project. Uses a C# 9 source generator.
License: MIT License
Generate classes based on example json files in your project. Uses a C# 9 source generator.
License: MIT License
Sometime, at the second compilation , it gives me
1>CSC : error SI0000: Exception: The hintName contains an invalid character ':' at position 13.
I think that is from
context.AddSource(GetSourceFileName(namespaceName, jsonFile.Path),
SourceText.From(generatedCode, Encoding.UTF8));
Could you modify
private string GetSourceFileName(string namespaceName, string path)
{
var fullPath = Path.GetFullPath(path)
.Replace(Path.GetFullPath("."), string.Empty)
.Replace("/", "")
.Replace("\", "")
.Replace(".", "_");
return $"{namespaceName}_{fullPath.Trim('_')}.gen.cs";
}
to take just the file name of the jsonFile ?
Something like
var fullPath = Path.GetFileNameWithoutExtension(path)
We do not need namespace and others - it is just a hint.
And , if we want to see, we can put in csproj
<PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath> </PropertyGroup>
If a folder is in an API
folder, use the folder name as part of the namespace.
I think this would help avoid any name conflicts.
IF I put the code in
C:\test2\JsonByExampleGenerator
the namespace generated by the JsonByExampleGenerator.Example.csroj will be
namespace JsonByExampleGenerator.Example.Json.C.Test2.JsonByExampleGenerator.JsonByExampleGeneratorExample.Products
Please put just the
namespace JsonByExampleGenerator.Example
Please add this header
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
and for the classes the
[GeneratedCode("...", "...")]
[CompilerGenerated]
to have intellisense
Hello Robin Hermanussen
I have started a mini-e-book about Roslyn Source Code Generators(RSCG).
Your RSCG , JsonByExampleGenerator from https://github.com/hermanussen/JsonByExampleGenerator/ , is between them at https://github.com/ignatandrei/RSCG_Examples/tree/main/JsonToClass
I am thinking to add to the e-book the some more information regarding the RSCG from the authors of RSCG.
So could you please answer to the following :
Short info about you , Robin Hermanussen
Why did you start this JsonByExampleGenerator ?
How do yourself use your JsonByExampleGenerator ?
What other RSCG do you use ?
Any other feedback ?
Thanks
Andrei Ignat
http://msprogrammer.serviciipeweb.ro/
Suggestion to make a private asset to avoid nuget errors like below:
NU1605: Detected package downgrade: Microsoft.CodeAnalysis.CSharp.Workspaces from 4.2.0 to 3.8.0. Reference the package directly from the project to select a different version.
NuGetizer is already setup this way.
First of all congratulation on such a great ides of generating strong type based on a sample Json file.
I am trying to use version 0.10.0 in a net6.0 project. Coding in the IDE looks fine and I can go to the definition of the c# code too. However I am getting the following error during the build.
[CS0234] The type or namespace name 'Json' does not exist in the namespace 'ConsoleApp1' (are you missing an assembly reference?)
Do you think it is because of net6.0 or something else.
Noticed you have been working on the upgrade to version 6.0 recently. Is there any plan for supporting net6.0?
Update:
I downgraded to version 0.8.4
, and it worked. It seems the last couple of packages pushed to nuget had issues.
What if, you could define json inline in code (or wrapped in an attribute or class) and have that available as an object at runtime? That would be sorta cool, would reduce the noise in fluent builders and such.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.