bool UGatherTextCommandletBase::WriteJSONToTextFile(TSharedPtr<FJsonObject> Output, const FString& Filename, TSharedPtr<FGatherTextSCC> SourceControl)
{
	bool WasSuccessful = true;

	// If the user specified a reference file - write the entries read from code to a ref file
	if ( !Filename.IsEmpty() && Output.IsValid() )
	{
		const bool DidFileExist = FPaths::FileExists(Filename);
		if (DidFileExist)
		{
			if( SourceControl.IsValid() )
			{
				FText SCCErrorText;
				if (!SourceControl->CheckOutFile(Filename, SCCErrorText))
				{
					UE_LOG(LogGatherTextCommandletBase, Error, TEXT("Check out of file %s failed: %s"), *Filename, *SCCErrorText.ToString());
				}
			}
		}

		if( WasSuccessful )
		{
			//Print the JSON data out to the ref file.
			FString OutputString;
			TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create( &OutputString );
			FJsonSerializer::Serialize( Output.ToSharedRef(), Writer );

			if (!FFileHelper::SaveStringToFile(OutputString, *Filename, FFileHelper::EEncodingOptions::ForceUnicode))
			{
				UE_LOG(LogGatherTextCommandletBase, Error, TEXT("Failed to write localization entries to file %s"), *Filename);
				WasSuccessful = false;
			}
		}

		if (!DidFileExist)
		{
			if( SourceControl.IsValid() )
			{
				FText SCCErrorText;
				if (!SourceControl->CheckOutFile(Filename, SCCErrorText))
				{
					UE_LOG(LogGatherTextCommandletBase, Error, TEXT("Check out of file %s failed: %s"), *Filename, *SCCErrorText.ToString());
				}
			}
		}
	}
	else
	{
		WasSuccessful = false;
	}

	return WasSuccessful;
}