Build-Time Type Generation
Automatically generate F# types from JSON files at build time using Fabulous.AST.Build.
This extension provides an MSBuild task that watches JSON files and generates F# record types during compilation.
Installation
|
💡 Tip: For programmatic control over generation, see the JSON Extension tutorial.
Quick Start
1. Add a JSON schema file
Create schemas/user.json:
|
2. Configure your project
Add to your .fsproj:
|
Note: Generated files are automatically included in compilation. No manual
<Compile Include="...">is needed.
3. Build
|
This generates Generated/user.Generated.fs:
type Root = {
id: int
name: string
email: string
isActive: bool
}
Configuration Options
Custom Root Type Name
Override the default Root type name:
|
Add a Module
Wrap types in a file-level module:
|
Generates:
module MyApp.Models
type User = { id: int; name: string; email: string; isActive: bool }
Note: Each file must have a unique module name since file-level modules cannot share the same fully-qualified name.
Custom Output Directory
Change the output location:
|
Custom Output Filename
Override the default {name}.g.fs pattern:
|
Multiple Files
Individual Configuration
|
Glob Patterns
Process all JSON files in a directory:
|
Type Inference Examples
Nested Objects
Input (company.json):
|
Output:
type Address = { street: string; city: string }
type Company = { name: string; address: Address }
Arrays
Input (users.json):
|
Output:
type UsersItem = { id: int; name: string }
type Users = UsersItem list
Optional Fields
Fields missing in some array objects become option types:
Input:
|
Output:
type RootItem = { id: int; name: string; nickname: string option }
type Root = RootItem list
Type Inference Rules
JSON Value |
F# Type |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Record type |
Special Field Name Handling
Leading Digits
Fields starting with digits are prefixed with _:
|
Generates: _2faEnabled: bool
Reserved Keywords
F# keywords are escaped with double backticks:
|
Generates: ``type``: string
Configuration Reference
Project Properties
Property |
Default |
Description |
|---|---|---|
|
|
Output folder for generated files (relative to project directory) |
|
|
Enable/disable generation (set to |
Note:
EnableFabulousAstJsondefaults totrue, so you only need to set it explicitly if you want to disable generation.
Item Metadata
Metadata |
Default |
Description |
|---|---|---|
|
|
Root type name |
|
(empty) |
File-level module name (e.g., |
|
|
Output filename |
Incremental Builds
The task uses content hashing for efficient builds:
- Files regenerate only when JSON content changes
- Configuration changes trigger regeneration
- Generated files include hash comments for verification
IDE Integration
Generated files are automatically included in compilation and placed before your source files, ensuring proper compile order.
IDE Recognition
Since generated files are added at project load time, most IDEs will recognize them after the first build. If your IDE doesn't see the generated types:
- Build the project once (
dotnet build) - Reload/refresh the project in your IDE
Tips
- Keep generated files in source control - Avoids first-build issues and ensures CI builds work immediately
- Don't delete generated files - Incremental build only regenerates when JSON changes
- Multi-targeting works automatically - Generation runs once before all target framework builds
Troubleshooting
IDE Not Seeing Generated Files
- Build the project:
dotnet build - Reload/refresh the project in your IDE
- Ensure the file exists in the
Generated/folder
File Not Updating
|
Disable Generation
Via project property:
|
Via command line:
|
Next Steps
- Learn about programmatic generation with Fabulous.AST.Json
- Explore the Fabulous.AST DSL for more code generation options
val int: value: 'T -> int (requires member op_Explicit)
--------------------
type int = int32
--------------------
type int<'Measure> = int
val string: value: 'T -> string
--------------------
type string = System.String
Fabulous.AST