Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hpc-bin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ian-Woo Kim
hpc-bin
Commits
6b6dea19
Commit
6b6dea19
authored
1 year ago
by
Leonard Zieger
Committed by
BinderDavid
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Add JSON functionality to Trace.HPC.Utils
parent
43e4db5f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Trace/Hpc/Utils.hs
+90
-0
90 additions, 0 deletions
src/Trace/Hpc/Utils.hs
with
90 additions
and
0 deletions
src/Trace/Hpc/Utils.hs
+
90
−
0
View file @
6b6dea19
{-# LANGUAGE GADTSyntax #-}
-- |
-- Module : Trace.Hpc.Utils
-- Description : Utility functions for hpc-bin
-- License : BSD-3-Clause
module
Trace.Hpc.Utils
where
import
Data.Char
(
isControl
)
import
qualified
Data.Map
as
Map
import
qualified
Data.Set
as
Set
import
Numeric
(
showHex
)
import
System.FilePath
import
Trace.Hpc.Flags
import
Trace.Hpc.Tix
...
...
@@ -80,3 +84,89 @@ mergeTix modComb f (Tix t1) (Tix t2) =
fm1
=
Map
.
fromList
[(
tixModuleName
tix
,
tix
)
|
tix
<-
t1
]
fm2
=
Map
.
fromList
[(
tixModuleName
tix
,
tix
)
|
tix
<-
t2
]
-- | Simple data type to represent JSON documents.
-- Copied from: https://hackage.haskell.org/package/ghc-9.4.4/docs/GHC-Utils-Json.html
data
JsonDoc
where
JSNull
::
JsonDoc
JSBool
::
Bool
->
JsonDoc
JSInt
::
Int
->
JsonDoc
JSString
::
String
->
-- | The 'String' is unescaped
JsonDoc
JSArray
::
[
JsonDoc
]
->
JsonDoc
JSObject
::
[(
String
,
JsonDoc
)]
->
JsonDoc
-- | Class for types which can be converted to JSON
class
ToJson
a
where
json
::
a
->
JsonDoc
-- | returns the JSON-data as a single line
jsonToString
::
JsonDoc
->
String
jsonToString
d
=
case
d
of
JSNull
->
"null"
JSBool
b
->
if
b
then
"true"
else
"false"
JSInt
n
->
show
n
JSString
s
->
doubleQuotes
$
escapeJsonString
s
JSArray
as
->
arrayToString
as
JSObject
o
->
objectToString
o
where
brackets
::
String
->
String
brackets
s
=
"["
++
s
++
"]"
braces
::
String
->
String
braces
s
=
"{"
++
s
++
"}"
doubleQuotes
::
String
->
String
doubleQuotes
s
=
"
\"
"
++
s
++
"
\"
"
arrayToString
::
[
JsonDoc
]
->
String
arrayToString
lst
=
brackets
$
go
lst
where
go
[]
=
""
go
[
x
]
=
jsonToString
x
go
(
x
:
xs
)
=
jsonToString
x
++
","
++
go
xs
objectToString
::
[(
String
,
JsonDoc
)]
->
String
objectToString
lst
=
braces
$
go
lst
where
go
[]
=
[]
go
[(
s
,
x
)]
=
doubleQuotes
s
++
":"
++
jsonToString
x
go
((
s
,
x
)
:
os
)
=
doubleQuotes
s
++
":"
++
jsonToString
x
++
","
++
go
os
escapeJsonString
::
String
->
String
escapeJsonString
=
concatMap
escapeChar
where
escapeChar
'
\b
'
=
"
\\
b"
escapeChar
'
\f
'
=
"
\\
f"
escapeChar
'
\n
'
=
"
\\
n"
escapeChar
'
\r
'
=
"
\\
r"
escapeChar
'
\t
'
=
"
\\
t"
escapeChar
'"'
=
"
\\\\\\\"
"
escapeChar
'
\'
'
=
"
\\\'
"
escapeChar
'
\\
'
=
"
\\\\\\\\
"
escapeChar
c
|
isControl
c
||
fromEnum
c
>=
0x7f
=
uni_esc
c
escapeChar
c
=
[
c
]
uni_esc
c
=
"
\\
u"
++
pad
4
(
showHex
(
fromEnum
c
)
""
)
pad
n
cs
|
len
<
n
=
replicate
(
n
-
len
)
'0'
++
cs
|
otherwise
=
cs
where
len
=
length
cs
-- | Writes JSON data to file
writeJSON
::
-- | Filepath to file
FilePath
->
-- | JSON data
JsonDoc
->
IO
()
writeJSON
fp
js
=
writeFileUtf8
fp
$
jsonToString
js
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment