I’m trying to create a library to deserialize a JSON into an object I’ve created a class for.
using System.Collections.Generic;
using UiPath.CodedWorkflows;
using Newtonsoft.Json;
namespace API_Library.RegisterCenter.CodedWorkflows.Vehicles
{
public class DeserializeResponse : CodedWorkflow
{
[Workflow]
public List<VehicleInfo> Execute(string JSONInput)
{
return JsonConvert.DeserializeObject<VehicleData>(JSONInput).VehicleInfos;
}
}
public class VehicleData
{
public List<VehicleInfo> VehicleInfos { get; set; }
}
public class VehicleInfo
{
public string LicenseNumber { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string UnregisteredDate { get; set; }
}
}
Everything works fine when debugging, here’s my workflow (I want to use my object later on)
But I’m unable to publish and the workflow analyzer is complaining:
Error Could not load file C:.…\API_Library_RegisterCenter\Vehicles\POST Vehicles.xaml. Reason: Cannot create unknown type ‘{http://schemas.microsoft.com/netfx/2009/xaml/activities}Variable({clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib}List({clr-namespace:API_Library.RegisterCenter.CodedWorkflows.Vehicles;assembly=API_Library.RegisterCenter.Core}VehicleInfo))’.
Do I need to have my classes in a “namespace” library and import it into this library to make it work, or am I doing something wrong?
I believe if you want to add a custom type to your UiPath low-code files you cannot simply create a cs file with the class in the project. You are going to need to make a class library in Visual Studio and install it as a Nuget package.
If you only use the custom class in your coded workflow, I do not believe this is required.
Through some trial and error I managed to get to work by generating a new library with just the two classes as separate Code Source Files ( I could probaly put the actual deserializer there too)
The question is if that’s a good way to do it or not.
I’m not sure what UiPath has in mind in terms of best practice around this.
Especially using custom classes.
This is how I do most of my stuff. No need to fiddle around with UiPath libraries or Custom Activities. The code is in a coded source file. Method DoStuff is static so no need to create an instance. But DoMoreStuff is not static so instead I create an instance called UtilsInstance to be able to use DoMoreStuff.
using System.Collections.Generic;
namespace API_Library.RegisterCenter.CodedWorkflows.DeserializeVehicles
{
public class VehicleData
{
public List<VehicleInfo> VehicleInfos { get; set; }
}
}
namespace API_Library.RegisterCenter.CodedWorkflows.DeserializeVehicles
{
public class VehicleInfo
{
public string LicenseNumber { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string UnregisteredDate { get; set; }
}
}
This is in a coded workflow
using System.Collections.Generic;
using UiPath.CodedWorkflows;
using Newtonsoft.Json;
namespace API_Library.RegisterCenter.CodedWorkflows.DeserializeVehicles
{
public class DeserializeResponse : CodedWorkflow
{
[Workflow]
public List<VehicleInfo> Execute(string JSONInput)
{
return JsonConvert.DeserializeObject<VehicleData>(JSONInput).VehicleInfos;
}
}
}
Error Could not load file C:.…\API_Library_RegisterCenter\Vehicles\POST Vehicles.xaml. Reason: Cannot create unknown type ‘{http://schemas.microsoft.com/netfx/2009/xaml/activities}Variable({clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib}List({clr-namespace:API_Library.RegisterCenter.CodedWorkflows.DeserializeVehicles;assembly=API_Library.RegisterCenter.Core}VehicleInfo))’.
But if I create the Coded Source File in a separate library and then import that library it works.
I think the other guy is pointing out you dont need a coded workflow and I agree. Just do that deserialize line in an assign. Making an entire workflow to run one line of code doesnt make sense in my opinion.
This version is still buggy with coded source files. I’d wager your error was the workflow analyzer throwing false positives and if you built via command line, which can skip the analyzer, it would work. I used to need to do this reguarly.
The classes can be anywhere, as long as the class is ‘public’ you can use it anywhere in your project as long as you have the relevant imported namespaces.
Sometimes its worth dumping out the coded workflow files generated in the .local folder in your project. Sometimes things go wrong there and you get ‘conflicts’.
In my experience this is mostly just the workflow analyzer being wrong as its a super annoying tool that frequently gives false positives, sometimes re-opening studio fixes it, sometimes I’ve had to publish via command line to skip it, and sometimes it gets confused by changes if the .locals isnt cleared out.
I’ve asked many times to UiPath for them to let me totally disable to analyzer, but sadly its still there.