0

I wrote a PowerShell script that inserts NewtonSoft JSON annotation above each line.

Starting file:

public class Rootobject
{
    public string clientID { get; set; }
    public string bankID { get; set; }
    public string applicationID { get; set; }
    //(...)
    public string appName { get; set; }
    public GeneralData loanDataRequest { get; set; }
}

public class Roles
{
    public string role1 { get; set; }
    public string otherParty1 { get; set; }
}

Result file that isn't correct:

public class Rootobject    
{

[JsonProperty(PropertyName = "")]
public string clientID { get; set; }

[JsonProperty(PropertyName = "")]
public string bankID { get; set; }

[JsonProperty(PropertyName = "")]        
public string applicationID { get; set; }
...
}
//other properties are exluded due to simplicity of the code

The script:

$FileName = "E:\startingFile.txt"
$FileOriginal = Get-Content $FileName
$lines = (Get-Content E:\startingFile.txt)
#trim lines
$newcontent = foreach ($line in $lines) {
    $line.Trim()
}

for ($i = 0; $i -lt $FileOriginal.Length; $i++) {
    if ($FileOriginal[$i] -like "*public*" -and $FileOriginal[$i] -notlike "*class*")  {
        # insert your line before this line and not insert if line contains '{','}'
        $FileOriginal[$i] -replace 'public', '`npublic'
        $FileOriginal[$i] -replace '{', '`n{'
        $NewFileContent += "`n[JsonProperty(PropertyName = """ + $FileOriginal[$i].Split()[2] + """)]"
    }

    $NewFileContent += $FileOriginal[$i]
}

$NewFileContent | Out-File "E:\resultFile.txt" 

Result file that I want to be:

public class Rootobject    
{

  [JsonProperty(PropertyName = "clientID ")]
  public string ClientID { get; set; }

  [JsonProperty(PropertyName = "bankID ")]
  public string BankID { get; set; }

  [JsonProperty(PropertyName = "applicationID ")]        
  public string ApplicationID { get; set; }
  ...
}
//other properties are exluded due to simplicity of the code

Questions:

  1. Why won't it add $FileOriginal[$i].Split()[2] to my JSON PropertyName?

    Answer (editied): I just realized that my line contains multiple blanks so for now I can get value like $FileOriginal[$i].Split()[10].

  2. How to replace my array $lines[3] element and to capitalize first letter? (public string clientID -> public string ClientID)

  3. How to properly format my txt output to be exactly the same formatted as startingFile?

1 Answer 1

1

Don't bother with inserting elements into an array or building an output string. Just insert the new lines into the output stream as you go. Also, if you want the indention preserved: don't trim the leading whitespace.

Get-Content 'E:\startingFile.txt' | ForEach-Object {
    if ($_ -like '*public*' -and $_ -notlike '*class*') {
        if ($_ -match '^(\s*)(public\s+\w+\s+)(\w)(\w*)(.*)') {
            '{0}[JsonProperty(PropertyName = "{1}{2}")]' -f $matches[1,3,4]
            $_ = '{0}{1}{2}{3}{4}' -f $matches[1], $matches[2], $matches[3].ToUpper(), $matches[4], $matches[5]
        }
    }
    $_
} | Set-Content 'E:\resultFile.txt'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.